> const curry = require('./curry')
> const add3 = (a, b, c) => a + b + c
> add3(1, 2, 3)
6
> const curAdd3 = curry(add3)
> curAdd3(1)
[Function]
> curAdd3(1, 2)
[Function]
> curAdd3(1, 2, 3)
6
> curAdd3(1)(2, 3)
6
> curAdd3(1)(2)(3)
6
> const nums = [1, 2, 3]
> curAdd3(...nums)
6
> curAdd3(...nums.slice(1))
[Function]
Last active
October 26, 2016 08:53
-
-
Save bradparker/ac02b83c34dc895c2d1df28ad3cc6d5a to your computer and use it in GitHub Desktop.
Currying is ok I guess
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
const curry = (f, prevArgs = []) => (...nextArgs) => { | |
const args = [...prevArgs, ...nextArgs] | |
if (f.length === args.length) { | |
return f(...args) | |
} else { | |
return curry(f, args) | |
} | |
} | |
module.exports = (f) => curry(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment