Last active
December 26, 2017 11:33
-
-
Save iuliaL/192f1df4ee6936b322d7a647c83942ba to your computer and use it in GitHub Desktop.
Transform some piece of data with an array of functions + Array.reduce
This file contains 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
// transformations as an array of arrow functions | |
// example for transformations of an array, in this case [1,2,3] | |
const pipeline = [ | |
// transformation functions here | |
array => { array.pop(); return array; }, | |
array => array.reverse() | |
]; | |
pipeline.reduce((xs, f) => f(xs), [1, 2, 3]); | |
// Generalization | |
const pipe = functions => data => { | |
return functions.reduce( | |
(resultSoFar, func) => func(resultSoFar), | |
data | |
); | |
}; | |
const pipeline = pipe([ | |
x => x * 2, | |
x => x / 3, | |
x => x > 5, | |
b => !b | |
]); | |
pipeline(5); | |
// true | |
pipeline(20); | |
// false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment