View compose.log.3.js
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 ) )
View showArgs.js
const f = ( ...args ) => {
console.log( 'f args: ', args )
console.log( 'f ...args: ', ...args )
}
// f args: [ 2, 3, 4 ]
// f ...args: 2 3 4
const showArgs = ( ...args ) => {
console.log( 'args: ', args )
console.log( '...args: ', ...args )
View pipe.example.05.js
const pipe = (...fns ) =>
fns.reduce( ( f, g ) => (...args ) => g( f(...args ) ) )
const add = ( before ) => [ before, ( x, y ) => x + y ]
const startFn = ( x, y ) => x + y
const fx1 = ( x ) => ( [ before = 0, op = startFn ] ) => op( before, x )
const fx2 = ( x ) => ( [ before = 0, op = startFn ] ) => op( before, x * x )
const fx3 = ( x ) => ( [ before = 0, op = startFn ] ) => op( before, x * x * x )
const start = () => []
View pipe.fxs.1.js
const startFn = ( x, y ) => x + y
const fx1 = ( x ) => ( [ before = 0, op = startFn ] ) => op( before, x )
const fx2 = ( x ) => ( [ before = 0, op = startFn ] ) => op( before, x * x )
const fx3 = ( x ) => ( [ before = 0, op = startFn ] ) => op( before, x * x * x )
const start = () => []
View pipe.fxs.0.js
const fx1 = ( x ) => ( [ before, op ] ) => op( before, x )
const fx2 = ( x ) => ( [ before, op ] ) => op( before, ( x * x ) )
const fx3 = ( x ) => ( before ) => x * x * x
View pipe.add.js
const add = ( before ) => [ before, ( x, y ) => x + y ]
View pipe.example.06.js
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
View pipe.example.03.js
const pipe = (...fns ) =>
fns.reduce( ( f, g ) => (...args ) => g( f(...args ) ) )
const add = ( before ) => [ before, ( x, y ) => x + y ]
const fx1 = ( x ) => ( [ before, op ] ) => op( before, x )
const fx2 = ( x ) => ( [ before, op ] ) => op( before, ( x * x ) )
const fx3 = ( x ) => ( ...before ) => x * x * x
const result = ( x ) =>
View pipe.example.02.js
const pipe = (...fns ) =>
fns.reduce( ( f, g ) => (...args ) => g( f(...args ) ) )
const fx1 = ( x ) => ( before = 0 ) => before + x
const fx2 = ( x ) => ( before = 0 ) => before + ( x * x )
const fx3 = ( x ) => ( before = 0 ) => before + ( x * x * x )
// f(x) = x^3 + x^2 + x
const result = ( x ) => pipe( fx3( x ), fx2( x ), fx1( x ) )()
View pipe.example.01.errado.js
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