Skip to content

Instantly share code, notes, and snippets.

@suissa
Last active October 15, 2017 08:53
Show Gist options
  • Save suissa/c890bfb4a280a162db2b9553b6a1d1a3 to your computer and use it in GitHub Desktop.
Save suissa/c890bfb4a280a162db2b9553b6a1d1a3 to your computer and use it in GitHub Desktop.
Demonstrando o poder da composição com fold
const def = ( x ) => typeof x !== 'undefined'
const identityFn = ( x ) => x
const compose = ( f, g ) => ( x ) => f( g( x ) )
const fold = ( fn, acc, [ x, ...xs ] ) =>
def( x ) && fold( fn, fn( acc, x ), xs ) || acc
const addOne = ( x ) => x + 1
const timesTwo = ( x ) => x * 2
const addThree = ( x ) => x + 3
const functions = [ addOne, timesTwo, addThree ]
const composeMany = ( xs ) => fold( compose, identityFn, xs )
const outputFn = composeMany( functions )
outputFn( 2 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment