Skip to content

Instantly share code, notes, and snippets.

@suissa
Last active October 21, 2017 22:20
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/63e22450d1327032c658232428eccf99 to your computer and use it in GitHub Desktop.
Save suissa/63e22450d1327032c658232428eccf99 to your computer and use it in GitHub Desktop.
const pipe = (...fns ) =>
fns.reduce( ( f, g ) => (...args ) => g( f(...args ) ) )
const add = ( before ) => [ before, ( x, y ) => x + y ]
const id = ( x ) => x
const other = ( x, y ) => y
const square = ( x ) => x * x
const cube = ( x ) => x * x * x
const startFn = ( x, y ) => x + y
const fx1 = ( x ) => ( [ before = 0, op = startFn ] ) => op( before, id( x ) )
const fx2 = ( x ) => ( [ before = 0, op = startFn ] ) => op( before, square( x ) )
const fx3 = ( x ) => ( [ before = 0, op = startFn ] ) => op( before, cube( x ) )
const start = () => []
const result = ( x ) =>
pipe(
start,
fx3( x ),
add,
fx2( x ),
add,
fx1( x )
)()
console.log( result( 2 ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment