Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Forked from ericelliott/pipe.js
Last active March 12, 2021 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/6ea145df4fa3b9147c745c97ff156a4f to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/6ea145df4fa3b9147c745c97ff156a4f to your computer and use it in GitHub Desktop.
Pipe
// Pipe takes the first argument and pipes it though each of the functions that you provide as the remaining arguments,
// and can be implemented as follows:
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
const fn1 = s => s.toLowerCase();
const fn2 = s => s.split('').reverse().join('');
const fn3 = s => s + '!'
const newFunc = pipe(fn1, fn2, fn3);
const result = newFunc('Time'); // emit!
// This function composes functions without applying them
// See; https://www.obvibase.com/dev-blog/i-ve-used-the-pipe-function-2-560-times-and-i-can-tell-you-it-s-good
const ltrCompose = (...fns) => (x) => fns.reduce((acc, el) => fn(acc), x);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment