Last active
January 1, 2019 23:30
-
-
Save thatkookooguy/08dae70b539e80dcd7cfd2c6aff8697a to your computer and use it in GitHub Desktop.
an example on how to create pipe functions that behave nicely when used
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
function sum(initialValue) { | |
// we need both of these here to allow returning | |
// the result if no number was added | |
innerSum.result = initialValue; | |
innerSum.toString = () => innerSum.result; | |
return innerSum; | |
function innerSum(addedValue) { | |
innerSum.result += addedValue; | |
return innerSum; | |
} | |
} | |
console.log(sum(2)(3)(4)); // 9 f (will print 9 but will note this is a function) | |
console.log(sum(2)(3)(4).result); // 9 (result returns an actual number) | |
console.log(sum(2)(3)(4) + 100); // 109 (when performing addition, toString is called) | |
let runningSummary = sum(5); | |
runningSummary(5)(40)(-5); | |
console.log(runningSummary); // 45 f | |
console.log(runningSummary.result); // 45 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment