Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment