Skip to content

Instantly share code, notes, and snippets.

@iuliaL
Last active December 26, 2017 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iuliaL/192f1df4ee6936b322d7a647c83942ba to your computer and use it in GitHub Desktop.
Save iuliaL/192f1df4ee6936b322d7a647c83942ba to your computer and use it in GitHub Desktop.
Transform some piece of data with an array of functions + Array.reduce
// 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