Last active
August 5, 2018 23:43
-
-
Save dlwjiang/99d61ed6ce3a3f27f1718824a7020c04 to your computer and use it in GitHub Desktop.
Example of JS partial application syntax.
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
//A function that returns a function | |
const adder = a => b => a + b | |
//Read `adder` left to right as params are passed in, | |
//by passing in 5 as `a` we create the function `b => 5 + b` | |
const add5 = adder(5); | |
//this is 8 | |
console.log(add5(3)); | |
//you can just chain together arguments without creating intermediate functions | |
console.log(adder(7)(9)); | |
//this is a function that returns a function that returns a function | |
const tripleAdder = a => b => c => a + b + c; | |
//this is a function that returns a function | |
//is equivalent to `b => c => 7 + b + c` | |
const doubleAdder7 = tripleAdder(7); | |
//equivalent to `c => 7 + 1 + c` | |
const add8 = doubleAdder7(1); | |
//this is 18 | |
console.log(add8(10)); | |
//this is 6 | |
console.log(tripleAdder(1)(2)(3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment