Advertisement
bennyp

A step towards functional fluency

Feb 27th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const tagsReducer = (tags, name) => {
  2.   const taggedAlready = tags.find(t => t.name === name);
  3.  
  4.   const incrementIfFound = t =>
  5.     t === taggedAlready
  6.       ? Object.assign(t, {count: t.count + 1})
  7.       : t;
  8.  
  9.   return [
  10.     ...tags.map(incrementIfFound),
  11.     ...(taggedAlready ? [] : [{name, count: 1}]),
  12.   ];
  13. };
  14.  
  15. const reduce = (f, init) => as => as.reduce(f, init);
  16. const flatten = (a, b) => [...a, ...b];
  17. const allTags =
  18.   compose(
  19.     reduce(tagsReducer, []),
  20.     reduce(flatten, []),
  21.     map(option([])),
  22.     map(prop('tags')),
  23.     option([]),
  24.     prop('clips')
  25.   );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement