Created
June 8, 2015 14:30
-
-
Save haskellcamargo/dab8b43298823fd24c78 to your computer and use it in GitHub Desktop.
Currying
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 curry$(f, bound){ | |
var context, | |
_curry = function(args) { | |
return f.length > 1 ? function(){ | |
var params = args ? args.concat() : []; | |
context = bound ? context || this : this; | |
return params.push.apply(params, arguments) < | |
f.length && arguments.length ? | |
_curry.call(context, params) : f.apply(context, params); | |
} : f; | |
}; | |
return _curry(); | |
} | |
// This makes any function curried: | |
var add = $curry(function(x, y) { | |
return x + y; | |
}); | |
add(1); // => function(y) { return **x** + y; } | |
add(1, 2); // 3 | |
add(1)(2); // 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment