Created
July 18, 2019 01:21
-
-
Save nathan-lapinski/8bd48cf158fa112c339e051f4483c35b to your computer and use it in GitHub Desktop.
curry
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(fn) { | |
const arity = fn.length; | |
return function $curry(...args) { | |
if (args.length < arity) { | |
return $curry.bind(null, ...args); | |
} | |
return fn.call(null, ...args); | |
}; | |
} | |
// some examples | |
function add(a,b,c) { | |
return a + b + c; | |
} | |
function one(a) { | |
return a + 1; | |
} | |
const cAdd = curry(add); | |
const cOne = curry(one); | |
console.log(cOne(1)) // 2 | |
console.log(cAdd(3)(2)(1)) // 6 | |
console.log(cAdd(5,6)(6)) // 17 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment