Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
  2. function curry(fn) {
  3.     const arity = fn.length;
  4.     return function $curry(...args) {
  5.         if (args.length < arity) {
  6.             return $curry.bind(null, ...args);
  7.         }
  8.         return fn.call(null, ...args);
  9.     };
  10. }
  11.  
  12. const match = curry((what, s) => s.match(what));
  13.  
  14. const replace = curry((what, replacement, s) => s.replace(what, replacement));
  15.  
  16. const filter = curry((f, xs) => xs.filter(f));
  17.  
  18. const map = curry((f, xs) => xs.map(f));
  19.  
  20.  
  21. match(/r/g, 'hello world'); // [ 'r' ]
  22. const hasLetterR = match(/r/g); // x => x.match(/r/g)
  23. hasLetterR('hello world'); // [ 'r' ]
  24. hasLetterR('just j and s and t etc'); // null
  25. filter(hasLetterR, ['rock and roll', 'smooth jazz']); // ['rock and roll']
  26. const removeStringsWithoutRs = filter(hasLetterR); // xs => xs.filter(x => x.match(/r/g))
  27. removeStringsWithoutRs(['rock and roll', 'smooth jazz', 'drum circle']); // ['rock androll', 'drum circle']
  28. const noVowels = replace(/[aeiou]/ig); // (r,x) => x.replace(/[aeiou]/ig, r)
  29. const censored = noVowels('*'); // x => x.replace(/[aeiou]/ig, '*')
  30. censored('Chocolate Rain'); // 'Ch*c*l*t* R**n'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement