Skip to content

Instantly share code, notes, and snippets.

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 wayneseymour/4e6a5ba5c5b02f287ef99f239c4b5d78 to your computer and use it in GitHub Desktop.
Save wayneseymour/4e6a5ba5c5b02f287ef99f239c4b5d78 to your computer and use it in GitHub Desktop.
const pipe = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
// Example
const addFoo = str => str + 'foo';
const addBar = str => str + 'bar';
const addFoobar = pipe(addFoo, addBar);
const addBarfoo = compose(addFoo, addBar);
addFoobar('hello ') // hello foobar
addBarfoo('hello ') // hello barfoo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment