Last active
August 29, 2015 14:05
-
-
Save hongymagic/ba94613e532d258de890 to your computer and use it in GitHub Desktop.
JavaScript curry and curryRight functions
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 apply(handler, args) { | |
return handler.apply(null, args); | |
} | |
function toArray(collection) { | |
return [].slice.call(collection); | |
} | |
function reverse(collection) { | |
return toArray(collection).reverse(); | |
} | |
function identity(arg) { | |
return arg; | |
} | |
function currier(rightward) { | |
return function (handler, arity) { | |
if (handler.curried) { | |
return handler; | |
} | |
arity = arity || handler.length; | |
var curry = function curry() { | |
var args = toArray(arguments); | |
if (args.length >= arity) { | |
var transform = rightward ? reverse : identity; | |
return apply(handler, transform(args)); | |
} | |
var inner = function () { | |
return apply(curry, args.concat(toArray(arguments))); | |
}; | |
inner.curried = true; | |
return inner; | |
}; | |
curry.curried = true; | |
return curry; | |
}; | |
} | |
var curry = currier(false); | |
var curryRight = currier(true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment