Skip to content

Instantly share code, notes, and snippets.

@suissa
Created October 21, 2017 20:07
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 suissa/ea9310a9b1c26556bfc2cb8da2fa9659 to your computer and use it in GitHub Desktop.
Save suissa/ea9310a9b1c26556bfc2cb8da2fa9659 to your computer and use it in GitHub Desktop.
const pipe = (...fns ) =>
fns.reduce( ( f, g ) => (...args ) => g( f(...args ) ) )
const fx1 = ( x ) => x
const fx2 = ( x ) => x * x
const fx3 = ( x ) => x * x * x
// f(x) = x^3 + x^2 + x
const result = pipe( fx3, fx2, fx1 )( 2 )
// fx3 = 2^3 = 8
// fx2 = 8^2 = 64
// fx1 = 64
console.log( result )
// 64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment