Created
April 30, 2018 13:55
-
-
Save rsolci/99fce77cb329798f5d88928b824396a3 to your computer and use it in GitHub Desktop.
A simple example of curried function
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 multiply = (...params) => { | |
if (params.length === 3) { | |
return params[0] * params[1] * params[2]; | |
} else { | |
return (...moreParams) => multiply(...[...params, ...moreParams]); | |
} | |
}; | |
const curry = fun => { | |
const parameterCount = fun.length; | |
return (...params) => { | |
if (params.length === parameterCount) { | |
return fun(...params); | |
} | |
return (...moreParams) => curry(fun)(...params, ...moreParams); | |
}; | |
}; | |
const multiplyCurried = curry((a, b, c) => a * b * c); | |
console.log(multiply(2, 3, 4)); | |
console.log(multiply(2)(3)(4)); | |
console.log(multiplyCurried(2, 3, 4)); | |
console.log(multiplyCurried(2)(3)(4)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment