Last active
July 31, 2018 01:14
-
-
Save cesarfrick/8eba96fccd8f6b4dd3cfb5150df24170 to your computer and use it in GitHub Desktop.
Different compose utility methods
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
| // 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