Skip to content

Instantly share code, notes, and snippets.

@rowlandekemezie
Created February 2, 2017 11:18
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 rowlandekemezie/c551af4fb9284f4b51ca9d4d30eb680d to your computer and use it in GitHub Desktop.
Save rowlandekemezie/c551af4fb9284f4b51ca9d4d30eb680d to your computer and use it in GitHub Desktop.
Write a function that takes multiple functions and pass the value of one to another
// Piping multiple functions
const _pipe = (f, g) => (...args) => g(f(...args));
const pipe = (...fns) => fns.reduce(_pipe);
const partials = (fn, ...args) => fn.bind(null, ...args);
const add2 = (...args) => args.reduce((a,b) => a + b);
const add3 = (a) => a + 6;
const add4 = (a, b) => a + b + 5;
// Using pipe and partials together
const test = pipe(add2, add3, partials(add4, 1));
test(1, 3, 5, 3, 6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment