Created
September 30, 2019 19:19
-
-
Save eakray/d229b6f3fab7656cdb08d45cef0e08fc to your computer and use it in GitHub Desktop.
Revisions
-
eakray created this gist
Sep 30, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ 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);