Last active
October 15, 2017 08:53
-
-
Save suissa/c890bfb4a280a162db2b9553b6a1d1a3 to your computer and use it in GitHub Desktop.
Demonstrando o poder da composição com fold
This file contains hidden or 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 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