Last active
February 10, 2017 12:45
-
-
Save renaudtertrais/f5f38bc8f34e7a8459b00f373331006f to your computer and use it in GitHub Desktop.
Simple ES6 partial function
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 partial = (fn, ...args) => partialApply(fn, args); | |
const partialApply = (fn, args) => fn.bind(null, ...args); | |
// example | |
const add = (a, b) => a + b; | |
const sum = (...args) => args.reduce(add, 0); | |
const add10 = partial(sum, 4, 6); | |
const add40 = partialApply(sum, [5, 15, 20]); | |
sum(1,2,3) // 6 | |
add10(10) // 20 | |
add40(add10()) // 50 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sandbox: http://jsbin.com/tiyezok/edit?js,console,output