Functional programming II —

Miki Lee
3 min readOct 23, 2020

--

Taken from pexels.com

I have written the article about functional programming in the previous blog. Here, I introduce my experience to use the functional programming, especially the currying and pipe, in my actual work.

Currying is a technique to convert function that has multiple arguments into a sequence of functions that takes a single argument(from Wiki). The more arguments the function has, the more responsibilities are given to the function. So the less argument the function has, the function can be much simpler and comply with the single responsibility more strictly. The following code is the very simple example of using the currying. If you look at the code (line 1–7), you would realize that the currying is taking advantage of the closure where the finally nested function has access to all outer functions’ variables or arguments. Basically, the sequential function in the currying returns the new function which has a aggregated lexical scope (line 1–7). From line 19, it shows how to curry functions by using lodash library.

Example of function currying

Pipeline is a directional sequence of functions loosely arranged so that the output of one is input into the next (from Functional programming in javascript). It is very similar to pipe used in Linux. But in order for pipeline to be possible, the linked functions have to be compatible in terms of argument number and type. So, the currying which takes only one argument is so important concept in the functional programming to compose the functions. The following code shows how to pipe your functions by using flow function of the lodash library. The two functions, add and square, are composed in a very readable way.

Example of function pipeline using flow function of lodash

Now, we are a little familiar with the currying and the pipe. From now on, it explains how to combine the currying and pipe with the following example code. The 3 functions, sum1, sum2 and sum3, are piped in the flow function of the lodash, the output of the previous function is passed to the next function as an argument. For example, the outcome of the sum1(1,2) is passed to the last parameter(d) of the sum2 function, likewise the outcome of the sum2(3,3) is passed to the last parameter( f) of the sum3 function. Like the Linux’ pipe, you would find that it flows very smoothly.

Example combining the currying and the pipe

Based on the knowledge introduced above, the following code shows how to apply the currying and pipe in real work. One of my tasks was to filter the objects based on the condition and then format them in a proper way. The extractYoungEmployees is the finally piped function which composes two functions, filterEmployees and formatEmployees. The filterEmployees accepts the youngEmployeeSelector as a callback function. Then, the output of the filterEmployees is passed to formatEmployees’s last parameter.

The functional programming is not so applicable as much as object-oriented programming currently. But, the more I learn the functional programming, I come to realize more significance of writing the readable and maintainable code, because one of the objectives of the functional programming is to create a readable code. So even if it is not many spaces to apply the functional programming in real code as of now, to gain the rational of it itself is sufficiently worthful process. I hope this article is a little of help to coders who study the functional programming.

--

--