Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. const data = ["one", "onehalf", "two", "twohalf", "three", "threehalf"];
  2. const f1 = item => item !== "one";
  3. const f2 = item => item !== "two";
  4. const f3 = item => item !== "three";
  5.  
  6. const filters = [f1, f2, f3];
  7.  
  8. //first approach (this does filters.length number of iterations)
  9.  
  10. const filteredData = (filters, data) => filters.reduce((d, f) => d.filter(f) , data)
  11.  
  12. console.log(filteredData(filters, data)); //["onehalf", "twohalf", "threehalf"]
  13.  
  14. //second approach (this does one iteration)
  15.  
  16. const toMap = f => reducing => (result, input) => reducing(result, f(input));
  17. const toFilter = predicate => reducing => (result, input) => (predicate(input) ? reducing(result, input) : result);
  18. const reducing = (array, value) => array.concat([value]);
  19. const transduce = (composition, reducing, initial, input) => input.reduce(composition(reducing), initial);
  20. const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
  21.  
  22. const transform = compose(
  23. toFilter(f1),
  24. toFilter(f2),
  25. toFilter(f3)
  26. );
  27.  
  28. const result = transduce(transform, reducing, [], data);
  29.  
  30. console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement