Skip to content

Instantly share code, notes, and snippets.

@rapha
Created April 28, 2009 05:42
Show Gist options
  • Save rapha/102970 to your computer and use it in GitHub Desktop.
Save rapha/102970 to your computer and use it in GitHub Desktop.
Currying in Javascript
function curry(func) {
function curried(args) {
return function(nextVal) {
var newArgs = args.concat([nextVal])
if (newArgs.length >= func.arity)
return func.apply(null, newArgs)
else
return curried(newArgs)
}
}
return curried([])
}
function log(level, message) {
print(level.toUpperCase() + ": " + message)
}
var info = curry(log)('INFO');
var warn = curry(log)('WARN');
info('I am partial to curried functions')
warn('May result in shorter code')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment