Skip to content

Instantly share code, notes, and snippets.

@cesarfrick
Last active July 31, 2018 01:14
Show Gist options
  • Select an option

  • Save cesarfrick/8eba96fccd8f6b4dd3cfb5150df24170 to your computer and use it in GitHub Desktop.

Select an option

Save cesarfrick/8eba96fccd8f6b4dd3cfb5150df24170 to your computer and use it in GitHub Desktop.
Different compose utility methods
// Simple composing (ES6)
// Belongs to Stefan A. Maric
const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x);
/*****************************/
// Recursive composing
// Belongs to Stefan A. Maric
const compose = (head, ...rest) => x => head ? head(compose(...rest)(x)) : x;
/****************************/
// Showing the compose function in a more pedagogical way
const compose = function (...fns) {
return function(init) {
return fns.reduceRight(function(accumulator, func) {
return func(accumulator);
}, init);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment