Skip to content

Instantly share code, notes, and snippets.

@eakray
Created September 30, 2019 19:19
Show Gist options
  • Select an option

  • Save eakray/d229b6f3fab7656cdb08d45cef0e08fc to your computer and use it in GitHub Desktop.

Select an option

Save eakray/d229b6f3fab7656cdb08d45cef0e08fc to your computer and use it in GitHub Desktop.

Revisions

  1. eakray created this gist Sep 30, 2019.
    30 changes: 30 additions & 0 deletions multifolds.js
    Original 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);