Skip to content

Instantly share code, notes, and snippets.

@bradparker
Last active October 26, 2016 08:53
Show Gist options
  • Save bradparker/ac02b83c34dc895c2d1df28ad3cc6d5a to your computer and use it in GitHub Desktop.
Save bradparker/ac02b83c34dc895c2d1df28ad3cc6d5a to your computer and use it in GitHub Desktop.
Currying is ok I guess
> 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]
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