Skip to content

Instantly share code, notes, and snippets.

@thatkookooguy
Last active January 1, 2019 23:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thatkookooguy/08dae70b539e80dcd7cfd2c6aff8697a to your computer and use it in GitHub Desktop.
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
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