Function composition
In the Functions currying recipe, we discovered a neat way of transforming a function to extract new functions from it. In this recipe, we are going to work on implementing the opposite transformation. It would be useful to have the option to merge a number of existing functions' declarations and define a new function from them. This is a common functional programming pattern called functions composition. Kotlin doesn't provide function composition mechanism out of the box. However, thanks to the extended built-in support for operations on functional types, we are able to implement a reusable mechanism for the composition manually.
Getting ready
In order to get familiar with function composition, let's study the following example. Let's say we have the following functions defined:
fun length(word: String) = word.length fun isEven(x:Int): Boolean = x.rem(2) == 0
The first one is responsible for returning the length of a given string. The second one checks whether a given...