O'Reilly logo

Mastering JavaScript Functional Programming by Federico Kereki

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Composing with higher order functions

It's pretty obvious that composing by hand could easily be done in a similar fashion as we saw above with pipelining. For example, the unique word counting function that we wrote a couple of sections earlier, could be written in simple JS 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', ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required