Functional programming by example with Kotlin

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.

Concepts :

  • Side Effect
  • Pure function
  • Function as a First-Class Citizen
  • Higher-Order Function
  • Closed Parameter and Closure
  • Curryfication / Partial Application
  • Lambda expression

Base method

From Imperative to Procedural

From Procedural to Functional : appearance of a Side Effect

Side effect: a

function or expression is said to have a side effect if it modifies some state outside its local environment.?—?wikipedia

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

return value is the same for the same arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams from I/O devices).

* 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.

First Currying

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

Using lambda is supposed to help us write more concise code, we see here that, with Kotlin, it is less obvious… JavaScript, for example, permits to remove curly braces and types to arrive to a very clean pipe.

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

Final form

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.

This article is inspired by this live coding talk(in french), I have adapted the code to Kotlin. An english version (with some little differences) can be found here.

Facebook Comments

More Stuff

Do you know these interesting programming languages features? As you know, in the Pragmatic Programmer, section Your Knowledge Portfolio, it is said thatLearn at least one new language every year. Different lan...
Just give me a sharp knife Google!* love being a developer, I love writing apps and I love writing good quality code. I call myself a software...
Android: Location Tracking with a Service Photo by Pathum Danthanarayana on UnsplashI had a goal in mind: I needed a GPS tracking app that would replace a my handheld GPS device as a locati...
10 Reasons to Learn Scala and Functional Programming Why learn Scala?One of the questions my readers often ask me is, “Should Java developers learn Scala? Does Scala have a better future than Java? Wh...
Spread the love

Posted by News Monkey