Last active
December 17, 2015 03:38
-
-
Save jmacias/5544541 to your computer and use it in GitHub Desktop.
Composable functions in Javascript with underscore.js contrib
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
// Not composable function: DON'T DO THIS! | |
function complexProcess() { | |
var a = getComponent(globalState), | |
b = subprocessOne(a), | |
c = subProcessTwo(a, b), | |
d = subProcessThree(a, b, c); | |
resetGlobalState(d); | |
} | |
// Composable Funtion | |
function complexProcess = _.pipeline(subprocessOne, | |
subProcessTwo, | |
subProcessThree); | |
var newState = complexProcess(state); // The state is an Object that we build up in the pipeline. | |
// Side efect is isolated. | |
resetGlobalState(newState); | |
// Is like piping in Unix, easy to test, easy to look at each funtion in isolation. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment