Skip to content

Instantly share code, notes, and snippets.

View lowderdev's full-sized avatar

Logan Lowder lowderdev

View GitHub Profile
function apply(x, fn) {
return fn(x);
}
function pipe(...functions) {
function newFunction(value) {
return functions.reduce(apply, value);
}
return newFunction;
@lowderdev
lowderdev / pipe-demo.js
Last active January 16, 2020 14:32
JavaScript Pipe Condensed
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