paint-brush
Functional programming by example with Kotlinby@jacquesgiraudel
7,913 reads
7,913 reads

Functional programming by example with Kotlin

by September 1st, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this article, I go through the <strong>base concepts</strong> of <strong>functional programming</strong> by showing the different steps of the refactoring of a method <strong>from imperative programming to functional</strong>. This article also permits to see <strong>where </strong>we can go with <strong>Kotlin as a FP language</strong>.

Company Mentioned

Mention Thumbnail
featured image - Functional programming by example with Kotlin
 HackerNoon profile picture

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 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). — wikipedia

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 — wikipedia

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. — wikipedia

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


jacquesgiraudel/IntroPgmFonct_Kotlin version of the Devoxx 2018 conf "Tout ce que vous avez toujours voulus savoir sur la programmation fonctionnelle…_github.com

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.