Created
January 12, 2021 21:27
-
-
Save diogojorgebasso/d6e66ecb707c94b2c71a7cb2ca625d40 to your computer and use it in GitHub Desktop.
How to create composition for N functions in Javascript
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
function composition(...functions){ | |
return function(value){ | |
return functions.reduce((acc, fn) =>fn(acc), value) | |
} | |
}//very flexible | |
function scream(text){ | |
return text.toUpperCase() | |
} | |
function emphasize(text){ | |
return `${text}!!!` | |
} | |
function splitChar(text){ | |
return text.split('').join(' ') | |
} | |
const actionPhrase = composition( | |
scream, | |
emphasize, | |
splitChar | |
) | |
//TEST: console.log(actionPhrase('Testing')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment