Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const data = ["one", "onehalf", "two", "twohalf", "three", "threehalf"];
- const f1 = item => item !== "one";
- const f2 = item => item !== "two";
- const f3 = item => item !== "three";
- const filters = [f1, f2, f3];
- //first approach (this does filters.length number of iterations)
- const filteredData = (filters, data) => filters.reduce((d, f) => d.filter(f) , data)
- console.log(filteredData(filters, data)); //["onehalf", "twohalf", "threehalf"]
- //second approach (this does one iteration)
- const toMap = f => reducing => (result, input) => reducing(result, f(input));
- const toFilter = predicate => reducing => (result, input) => (predicate(input) ? reducing(result, input) : result);
- const reducing = (array, value) => array.concat([value]);
- const transduce = (composition, reducing, initial, input) => input.reduce(composition(reducing), initial);
- const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
- const transform = compose(
- toFilter(f1),
- toFilter(f2),
- toFilter(f3)
- );
- const result = transduce(transform, reducing, [], data);
- console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement