Created
October 21, 2017 23:04
-
-
Save suissa/20a395e7653f1d3b352731940f464bc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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