Skip to content

Instantly share code, notes, and snippets.

@jmacias
Last active December 17, 2015 03:38
Show Gist options
  • Save jmacias/5544541 to your computer and use it in GitHub Desktop.
Save jmacias/5544541 to your computer and use it in GitHub Desktop.
Composable functions in Javascript with underscore.js contrib
// 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