Skip to content

Instantly share code, notes, and snippets.

@SeanJM
Created October 26, 2015 12:49
Show Gist options
  • Save SeanJM/4f52055bc3fe38fe7e13 to your computer and use it in GitHub Desktop.
Save SeanJM/4f52055bc3fe38fe7e13 to your computer and use it in GitHub Desktop.
// A function which which allows you to apply a list of functions (supplied as arguments) to a value.
//
// Usage: pipe(functions...)(value)
//
// Example: var getFirstWordLetter = pipe(getFirstWord, getFirstLetter);
// getFirstWordLetter('Sean MacIsaac')
// -> 'S'
function pipe () {
var functionList = [].slice.call(arguments);
function _init_() {
var rest = functionList.slice(1);
var value = functionList[0].apply(null, arguments);
if (rest.length) {
return pipe.apply(null, rest)(value);
}
return value;
}
return _init_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment