Functional Programming By Example

I had a go at this myself, without the dependency requirement (and less code): Babel REPL => (...a) => {%0A let x = args.concat(a);%0A return x.length >= fn.length ?%0A fn(...x) :%0A curry(fn, x);%0A}%0Aconst compose = (...a) => x => a.reduceRight((y, fn) => fn(y), x);%0A%0A/%0A * Setup some curried implementations here.%0A */%0Aconst sort = curry((fn, x) => Array.from(x).sort(fn));%0Aconst reduce = curry((fn, y, x) => x.reduce(fn, y));%0Aconst split = curry((by, x) => x.split(by));%0Aconst take = curry((len, x) => x.slice(0, len));%0Aconst reverse = x => x.reverse();%0A%0A/%0A * Define function to split string into words.%0A /%0Aconst splitByWord = split(/[\s.,/:\n]%2B/);%0A%0A/%0A * Reducer to get the word count. %0A */%0Aconst getWordCounts = reduce((obj, word) => {%0A obj[word] = (obj[word] || 0)%2B1;%0A return obj;%0A}, {});%0A%0A/%0A * Render the top 10 to a string.%0A */%0Aconst render = reduce((str, [ word, count ], i) => {%0A return str %2B= ${i %2B 1}: ${word} :- ${count}\n;%0A}, 'top 10\n====\n');%0A%0A/%0A * Get the top 10.%0A */%0Aconst getTop10 = compose(%0A take(10),%0A reverse,%0A sort(([, a], [, b]) => a - b),%0A Object.entries%0A);%0A%0A/*%0A * The program.%0A */%0Aconst program = compose(%0A render,%0A getTop10,%0A getWordCounts,%0A splitByWord%0A);%0A%0Aconsole.log(program('hello world forever pooh and hello another hello with world pooh without another hesitation agreed for for'));%0A)

/r/javascript Thread Link - tobyho.com