Davide Mendolia

Thinking about functional programming in Kotlin

In our way to Lambda World, we were talking about the Jorge Castillo article about Kotlin Functional Programming: Does it make sense?, and we wanted to give our opinion on some points, but first of all, we want to give him a big thanks for his articles about Kotlin and functional programming, to empower the community to share knowledge about these topics.

Lazy evaluation

Having the possibility to pass functions as function arguments, or return functions from other functions opens the language to new techniques like deferred computation. If you return functions on your different layers instead of already computed values, what you get back from it is a composed deferred computation, or in other words, lazy evaluation. Your computations are deferred in time, still not run.

-- Jorge Castillo

Here we are mixing two concepts: the usage of higher-order functions and lazy evaluation, they can work together but they are not the same thing.

Let's review what lazy evaluation is generally in non-FP programming languages.

var c = a && (1 / 0) == 1

Boolean operators generally use short-circuit evaluation to avoid checking the second operand if the first one is false in the case of && or the if the first operand is true in the case or ||.

if (a > b) {
    max = a + 1 
} else {
    max = b + 1
}

In our second example the control structure will only evaluate one branch depending on the value of evaluating a > b. This is also lazy evaluation, we don't evaluate both branches and keep values and state of the branch corresponding to control statement.

define max(a, b, c) = if a then b else c
l = f(a > b, a + 1, b + 1)

In this last example, we will see the difference between an eager and a lazy language. In a lazily evaluated language, we will only evaluate the param b or c when the branch of the if is chosen based on the value of a.
As mentioned by @JorgeCastilloPr, in eager languages we can make part of our code explicitly lazy by wrapping our params a and b as functions to force the laziness.

define max(a, b, c) = if a then b() else c()
l = f(a > b, { a + 1 }, { b + 1 })
  • Does Kotlin have Lazy Evaluation? Just as other eager languages.
  • Is it important to highlight this property in Kotlin? Not really, I would rather focus on the high order function concept that is explained in the article.

Higher-order function

A Higher-order function is a function that has at least one of the following properties:

  • takes one or more functions as parameter
  • returns a function
fun <T> twice(l: (T) -> T): (T) -> T = { l(l(it)) }
fun f(xxx: String) = "Cool " + xxx
println(twice(::f)("Party")) // Cool Cool Party

@JorgeCastilloPr mentions some examples like collection operations as map, flatMap, fold, reduce that maybe you are more familiar with and you are using in your everyday job.

Functional Programming language

Does it mean it’s a Functional Programming language? Probably not, or probably yes ¯\_(ツ)_/¯. I mean, what a functional programming lang is, is not clearly defined and it depends on where you look at or which posts do you read. The boundaries for that could be a bit blurry. But honestly, that is not such important here.
-- Jorge Castillo

  • Is Kotlin a Functional programming languages? I don't think so, it wasn't designed to be one and it lets you operate and implement in more paradigm than languages like Clojure or Haskell would let you.
  • Can you program in Kotlin in a functional style? Yes, as kotlin has a lot of features that allow you that, but PHP and C++11 also allows you to program in a functional style too.

New languages like Kotlin or Scala are bringing more paradigms and this means more possibilities for a programmer to implement a solution and transform ideas in products. What a great moment to be a programmer!