Skip to content

Instantly share code, notes, and snippets.

@jmaicaaan
Created August 23, 2020 05:56
Show Gist options
  • Save jmaicaaan/4d6fb4ddeec3ae1b1a6ec1c812136a6b to your computer and use it in GitHub Desktop.
Save jmaicaaan/4d6fb4ddeec3ae1b1a6ec1c812136a6b to your computer and use it in GitHub Desktop.
/**
*
* These utility functions are often available on various
* functional libraries (i.e ramdajs)
* `compose` runs from right-to-left
* `pipe` runs from left-to-right
*/
const compose = (...fns) => (initialValue) => fns.reduceRight((d, f) => f(d), initialValue);
const pipe = (...fns) => (initialValue) => fns.reduce((d, f) => f(d), initialValue);
const data = 5;
const addBy1 = (x) => x + 1;
const multiplyBy2 = (x) => x * 2;
const composeResult = compose(multiplyBy2, addBy1)(data);
// 12
const pipeResult = pipe(addBy1, multiplyBy2)(data);
// 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment