In this article, I go through the base concepts of functional programming by showing the different steps of the refactoring of a method from imperative programming to functional. This article also permits to see where we can go with Kotlin as a FP language.
- Side Effect
- Pure function
- Function as a First-Class Citizen
- Higher-Order Function
- Closed Parameter and Closure
- Curryfication / Partial Application
- Lambda expression
From Imperative to Procedural
From Procedural to Functional : appearance of a Side Effect
Side effect: a
With FP, we do not want to have side effects as we want to have isolated processes to enable parallelization. A function that has no side effect (and do return the same value for same arguments) is called a pure function.
Pure function: has the following properties:
* Its evaluation has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams). —
Moving up the Side Effect
With this step, we start to use a function like a variable (passed as an argument to other functions, returned by another function or assigned as a value to a variable).
This usage is called functions as a first-class citizen
A function which takes an other function as argument is called a higher-order function.
add() uses a parameter, results, which is not passed as an argument. This parameter is called a closed parameter and the method a closure. The parameter is said closed because not modifiable by argument, by opposition to an open parameter like those passed as standard method argument.
Removing the Side Effect
Finally, the side effect is completely removed by using the plus operator which returns another list with the additional movie.
Currying: technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument —
Currying enables lambda calculus which you will probably never use in standard apps but also permits to reduce functions to less complex ones which may already exist in your SDK and then reduce your code.
Partial application is a similar concept without the 1-argument requirement.
Partial application: refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. —
(Removing Movie duplicate)
From anonymous to Lambda expression and last currying
Lambda expression: anonymous function with simplified notation
An alternative exists int Kotlin with typed variable notation (see the end of the article).
Code reduction by replacing to a base function : Collection.Filter
The code is now an expression of very basic functions, the business is clearly separated from the rest ( findByTitle() and matches()). We obtain a true functional program.
Appendix : with typed variable notation
The type can be removed from the pipe to the variable. With long pipes, it is still not friendly and can also lead to type errors.
The code is on github with unit tests to ensure the code is working between steps.