Skip to content

Instantly share code, notes, and snippets.

@lucaschain
Last active July 12, 2019 14:06
Show Gist options
  • Save lucaschain/ef7698a6f10008b02a1862ee961feb0b to your computer and use it in GitHub Desktop.
Save lucaschain/ef7698a6f10008b02a1862ee961feb0b to your computer and use it in GitHub Desktop.
functional pure compose and pipe in javascript
const compose = (...funcs) => (
funcs.reduceRight((accumulated, current) => (
x => current(accumulated(x))
))
)
const pipe = (...funcs) => (
funcs.reduce((accumulated, current) => (
x => current(accumulated(x))
))
)
const sum2 = x => x + 2
const mult3 = x => x * 3
const mult3AndSum2 = compose(sum2, mult3);
const sum2AndMult3 = pipe(sum2, mult3);
console.log(sum2AndMult3(1) === 9) //true
console.log(mult3AndSum2(1) === 5) //true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment