Created
April 30, 2018 15:28
-
-
Save jeffschwartz/fd0824f38de45000663b40e4d00e132a to your computer and use it in GitHub Desktop.
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
const curry = (fn) => { | |
var a = []; | |
var $curry = (...args) => { | |
var tooManyArgsException = n => | |
`too many args exception: curry expected ${fn.length} args; found ${n} args`; | |
a = [...a, ...args]; | |
// reset if already has fulfilled arity requirement | |
a = a.length > fn.length ? args : a; | |
if (a.length > fn.length) { | |
throw tooManyArgsException(a.length); | |
} | |
if (a.length < fn.length) { | |
return $curry; | |
} | |
return fn(...a); | |
} | |
return $curry; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment