Last active
July 12, 2019 14:06
-
-
Save lucaschain/ef7698a6f10008b02a1862ee961feb0b to your computer and use it in GitHub Desktop.
functional pure compose and pipe in javascript
This file contains hidden or 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 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