Function composition
In the previous section, we saw an example of higher-order functions that could accept two different functions and execute them in a predefined order. This function was not so flexible in the sense that it would break if we wanted to combine two accepted functions differently. Function composition can solve this issue and make it even more flexible. To present this concept, we will examine an example of non-functional composition first, and then we will introduce functional composition.
Suppose that, in our application, we need to interact with a backend RESTful API and receive a String
value that contains a list of prices in order. The backend RESTful API is being developed by a third-party and is not designed properly. Unfortunately, it returns a String
with numbers in it separated by commas, as follows:
"10,20,40,30,80,60"
We need to format the content that we receive before using it. We will extract elements from String and create an array, and then we will append...