Created
August 5, 2019 16:02
-
-
Save rachelcarmena/196b9f8b1c6b078b65fb1328f09c0269 to your computer and use it in GitHub Desktop.
A possible curry function in JS
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 => { | |
const arity = fn.length; | |
const _curry = (...args) => | |
args.length === arity | |
? fn(...args) | |
: arg => _curry(...args, arg); | |
return _curry(); | |
} | |
// My first version before refactoring: | |
const curry = fn => { | |
const arity = fn.length; | |
const _curry = array => { | |
// I call the function when having all the arguments | |
if (array.length == arity) | |
return fn(...array); | |
// I create a function with only one argument | |
else | |
return arg => _curry(array.concat(arg)); | |
} | |
return _curry([]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment