Created
August 29, 2015 13:34
-
-
Save johnkpaul/82b5513768a65f6c4dcc to your computer and use it in GitHub Desktop.
My solution to https://github.com/frantic/currying
This file contains 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 add() { | |
var args = [].slice.call(arguments); | |
var sum = args.reduce(function(a, b){ return a + b; }, 0); | |
return numify(sum); | |
function numify(num){ | |
var ret = function(a){ | |
return a === undefined ? numify(num) : numify(a + num); | |
} | |
ret.valueOf = function(){ return num; }; | |
return ret; | |
} | |
} | |
test(add(1, 2), 3); | |
test(add(3)(4)(), 7); | |
test(add(3)(4)(5), 12); | |
var three = add(3); | |
var four = add(4); | |
test(three, 3); | |
test(four, 4); | |
test(three(5), 8); | |
test(three(6), 9); | |
test(three(four), 7); | |
test(three(four)(three(four)), 14); | |
function test(a, b) { | |
if (a == b) { | |
console.log('OK'); | |
} else { | |
console.log(a + ' != ' + b); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment