Skip to content

Instantly share code, notes, and snippets.

@princefishthrower
Last active June 1, 2021 09:36
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 princefishthrower/21f96f9224de98a061142f188d852215 to your computer and use it in GitHub Desktop.
Save princefishthrower/21f96f9224de98a061142f188d852215 to your computer and use it in GitHub Desktop.
pipe.js - execute as many functions as needed in series
// pipe.js - execute as many functions as needed in series
// from https://egghead.io/lessons/react-create-a-pipe-function-to-enable-function-composition
// usage:
// const addTwo = (a, b) => console.log(a + b);
// const addThree = (a, b, c) => console.log(a + b + c);
// pipe(addTwo(1, 2), addThree(3, 4, 5));
// 3
// 12
const combine = (f, g) => (...args) => g(f(...args));
export const pipe = (...fns) => fns.reduce(combine);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment