Created
January 10, 2016 16:34
-
-
Save learningjs/75a75d34e8083705df38 to your computer and use it in GitHub Desktop.
Un-bind your JS with curry - https://medium.com/@wilmoore/un-bind-your-js-with-curry-a8657a4138cb#.i3hf8zc42
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function map (fn, list) { | |
var end = list.length | |
var idx = -1 | |
var out = [] | |
while (++idx < end) { | |
out.push(fn(list[idx])) | |
} | |
return out | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// math.js | |
'use strict' | |
// NOTE: remember to `npm install curry2` | |
const curry2 = require('curry2') | |
exports.binaryAdd = curry2(binaryAdd) | |
/** | |
* Addition of two integers. | |
* | |
* @param {Number} n1 | |
* First integer. | |
* | |
* @param {Number} n2 | |
* Second integer. | |
* | |
* @return {Number} | |
* Sum of adding two integers. | |
*/ | |
function binaryAdd (n1, n2) { | |
return n1 + n2 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// math.js | |
'use strict' | |
exports.binaryAdd = binaryAdd | |
/** | |
* Addition of two integers. | |
* | |
* @param {Number} n1 | |
* First integer. | |
* | |
* @param {Number} n2 | |
* Second integer. | |
* | |
* @return {Number} | |
* Sum of adding two integers. | |
*/ | |
function binaryAdd (n1, n2) { | |
return n1 + n2 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const add = require('./math').binaryAdd | |
const integers = [3, 4, 5] | |
map(add(10), integers) | |
// => [ 13, 14, 15 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const add = require('./math').binaryAdd | |
const integers = [3, 4, 5] | |
map(n => add(10, n), integers) | |
// => [ 13, 14, 15 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const add = require('./math').binaryAdd | |
const add10 = add.bind(null, 10) | |
const integers = [3, 4, 5] | |
map(add10, integers) | |
// => [ 13, 14, 15 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment