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
function apply(x, fn) { | |
return fn(x); | |
} | |
function pipe(...functions) { | |
function newFunction(value) { | |
return functions.reduce(apply, value); | |
} | |
return newFunction; |
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 pipe = (...fns) => x => fns.reduce((y, f) => f(y), x); | |
const add3 = x => x + 3; | |
const times2 = x => x * 2; | |
const add3times2 = pipe(add3, times2); | |
const times2add3 = pipe(times2, add3); | |
console.log(add3times2(5)); // 16 | |
console.log(times2add3(5)); // 13 |