Writing Functional Programs

Learn how to use higher-order functions and composition to write functional code.

Higher-order functions and composition

Let’s return to the topic of building applications. Higher-order functions are the first way of building larger programs out of smaller components. With the withFilterAndMap function (discussed here), we could’ve created three one-liner functions to be used by the withFilterAndMap function in combination with map and filter.

Together, these functions produce the same results as a loop of 12 lines. Therefore, we get shorter, cleaner, and more reusable code.

There are other ways to compose functions. We’ve already discussed one briefly. We can combine functions by putting them one after the other, with the output of the first becoming the input of the next, as shown in the example below.

Get hands-on with 1400+ tech skills courses.