Last active
August 21, 2016 16:26
-
-
Save npatmaja/022506c01d1d0501a2679abb151ef4f2 to your computer and use it in GitHub Desktop.
Functional 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
// Partial application | |
const sum = (a, b, c) => a + b + c; | |
const papply = (fn, ...rest) => (...last) => fn.apply(null, [...rest, ...last]); | |
const a = papply(sum, 5); | |
console.log(a(2, 3)); | |
// currying | |
const curry = (fn, ...rest) => { | |
const ln = fn.length; | |
const fnArgs = [...rest]; | |
const accumulator = () => { | |
if ((ln - fnArgs.length) <= 0) { | |
return fn.apply(null, fnArgs); | |
} | |
return (x) => { | |
if (x) { | |
fnArgs.push(x); | |
} | |
return accumulator(); | |
} | |
} | |
return accumulator(); | |
} | |
const sum = (a, b, c) => a + b + c; | |
const add5 = curry(sum, 2, 3); | |
console.log(add5()(10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment