Composing with higher-order functions

It's pretty obvious that composing by hand could easily be done in a similar fashion to pipelining. For example, the unique word counting function that we wrote previously could be written in simple JavaScript style:

const getUniqueWords1 = str => {  const str1 = removeNonAlpha(str);  const str2 = toUpperCase(str1);  const arr1 = splitInWords(str2);  const set1 = arrayToSet(arr1);  const arr2 = setToList(set1);  return arr2;};

Alternatively, it could be written more concisely (and more obscurely!) in one-liner style:

const getUniqueWords2 = str =>  setToList(arrayToSet(splitInWords(toUpperCase(removeNonAlpha(str)))));console.log(getUniqueWords2(GETTYSBURG_1_2));// [ 'A', 'AGO', 'ALL', 'AND', ... 'WAR', 'WE', ...

Get Mastering JavaScript Functional Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.