Created
April 28, 2009 05:42
-
-
Save rapha/102970 to your computer and use it in GitHub Desktop.
Currying in Javascript
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 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