Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
- function curry(fn) {
- const arity = fn.length;
- return function $curry(...args) {
- if (args.length < arity) {
- return $curry.bind(null, ...args);
- }
- return fn.call(null, ...args);
- };
- }
- const match = curry((what, s) => s.match(what));
- const replace = curry((what, replacement, s) => s.replace(what, replacement));
- const filter = curry((f, xs) => xs.filter(f));
- const map = curry((f, xs) => xs.map(f));
- match(/r/g, 'hello world'); // [ 'r' ]
- const hasLetterR = match(/r/g); // x => x.match(/r/g)
- hasLetterR('hello world'); // [ 'r' ]
- hasLetterR('just j and s and t etc'); // null
- filter(hasLetterR, ['rock and roll', 'smooth jazz']); // ['rock and roll']
- const removeStringsWithoutRs = filter(hasLetterR); // xs => xs.filter(x => x.match(/r/g))
- removeStringsWithoutRs(['rock and roll', 'smooth jazz', 'drum circle']); // ['rock androll', 'drum circle']
- const noVowels = replace(/[aeiou]/ig); // (r,x) => x.replace(/[aeiou]/ig, r)
- const censored = noVowels('*'); // x => x.replace(/[aeiou]/ig, '*')
- censored('Chocolate Rain'); // 'Ch*c*l*t* R**n'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement