Function composition is an operation that takes functions f
and g
and computes them as f(g(x))
. The input data is passed first to g
and then the result of that data is passed to f
. We learned all about Ramda passing data last and currying. These concepts were introduced for the ease of applying function composition.
Consider an array of students. We want to return the names of the top 3 performers.
Here, we had to use array destructuring for students because sort mutates the input array. To do the same in functional programming, we would write functions for this logic as follows:
We would now apply function composition using the above functions.
This nested manner of doing functional composition starts getting unwieldy the more operations we need to do on the input. Ramda provides a simple way to make functional composition much more readable. We can make use of the pipe
function.
We just pass in the list of functions we need, and then pass in the curried data at the end. pipe
takes care of passing the output of each function as the input of the next function. It does so in a left-to-right manner, i.e, input data is passed to the left-most function first and its output is passed to the next function.
But, there is an error with the above code. While pipe
passes the students
array as curried data to our three functions, the functions themselves aren't curried. Thus we would not be able to execute the above code.
We can make use of the curry
function from Ramda to return the curried version of our functions as follows:
Now getTopThreeNames
would work as expected. This is the power of Ramda when it comes to the functional programming approach. It makes it much easier to write readable and elegant code in the functional style with features like currying and functional composition.
Let's learn about more Ramda functions in the next chapters.