an illustration of functional programming
courtesy of oracle blogs

Understanding Kotlin’s Lambda Expressions and Higher Order Functions Part 1(b)

Matheka Mwanzia
DevCNairobi
Published in
2 min readFeb 25, 2020

--

In the previous article, we took a slight dive into lambda expressions. In this article, we shall further take a look at higher order functions in Kotlin.

Kotlin treats functions as first class citizens; this means that functions can be stored in variables or data structures. In a nutshell, you can play around with functions in a way not possible to achieve with other non-function values.

To achieve this, Kotlin provides a set of specialized language constructs like lambda expressions to represent functions.

Now that you’ve had a clear overview of how Kotlin utilizes functions, let’s try and understand higher order functions.

Higher Order Functions

Higher order functions are functions that take functions as parameters, return functions, or do both. That’s straightforward, ain’t it?.

Now check the syntax below:

fun addPassengers(block: () -> Unit) {}

The function above accepts a function() that returns Unit. Unit translates to Void in Java, meaning Void in Kotlin is obliterated.

The block in the function addPassenger is the parameter name

i.e

fun doSomething(ruaraka: () -> Unit) {ruaraka()}

Now with that, you have a basic understanding of higher order functions. In the subsequent articles on this subject, we shall delve further and master this concept.

--

--

Matheka Mwanzia
DevCNairobi

Android developer, Entrepreneur, Tech, Politics, Technical Writer (Facebook Developers Circle: Nairobi)