Skip to content

Instantly share code, notes, and snippets.

@suissa
Created October 21, 2017 23:04
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/20a395e7653f1d3b352731940f464bc7 to your computer and use it in GitHub Desktop.
Save suissa/20a395e7653f1d3b352731940f464bc7 to your computer and use it in GitHub Desktop.
const compose = (...fns ) =>
fns.reduce( ( f, g, i ) => (...args ) => {
console.log( '\ni:', i )
console.log( 'args:', ...args )
console.log( 'f:', f )
console.log( 'g:', g )
return f( g(...args ) )
} )
const add1 = ( x ) => x + 1
const add4 = ( x ) => x + 1
const add7 = ( x ) => x + 7
const times2 = ( x ) => x * 2
const times5 = ( x ) => x * 5
compose( add4, times2, add1, add7, times5 )( 2 )
// i: 4
// args: 2
// f: (...args) => {
// console.log('\ni:', i);
// console.log('args:', ...args);
// console.log('f:', f);
// console.log('g:', g);
// return f(g(...args));
// }
// g: x => x * 5
// i: 3
// args: 10
// f: (...args) => {
// console.log('\ni:', i);
// console.log('args:', ...args);
// console.log('f:', f);
// console.log('g:', g);
// return f(g(...args));
// }
// g: x => x + 7
// i: 2
// args: 17
// f: (...args) => {
// console.log('\ni:', i);
// console.log('args:', ...args);
// console.log('f:', f);
// console.log('g:', g);
// return f(g(...args));
// }
// g: x => x + 1
// i: 1
// args: 18
// f: x => x + 1
// g: x => x * 2
// => 37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment