Skip to content

Instantly share code, notes, and snippets.

@spencewood
Last active December 30, 2015 19:09
Show Gist options
  • Save spencewood/7872039 to your computer and use it in GitHub Desktop.
Save spencewood/7872039 to your computer and use it in GitHub Desktop.
Pipeline mixin for underscore/lodash. Takes a seed as an initial value and any number of functions as parameters. The seed value is passed to the first function, and the return value of each function is passed as the parameter to the next.
_.mixin({
pipeline: function (seed) {
return _.rest(_.toArray(arguments)).reduce(function (l, r) {
return r(l);
}, seed);
}
});
@spencewood
Copy link
Author

Example:

_.pipeline([1,1,2,2,3,3,4,4], _.uniq, _.rest, _.first) // 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment