Created
September 30, 2019 19:19
-
-
Save eakray/d229b6f3fab7656cdb08d45cef0e08fc to your computer and use it in GitHub Desktop.
approaches to multiple folds
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
| 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment