Functional Programming in Kotlin (Part 1)— Higher-Order Functions

Sharjeel Haider
MindOrks
Published in
5 min readOct 16, 2019
Photo by Maria Freyenbacher on Unsplash

I will teach you how to do functional programming in Kotlin

In this first part of the series, I will begin by telling you about one of the most important concepts in functional programming and that is Higher-Order Functions.

I will talk about what higher-order functions are and I will then move on to code and show you how to use them.

Functional programming has made programming so much more fun to me. If you have been programming for a while but haven’t yet tried your hand at functional programming before, this series is going to change your life. Learning to program functionally is gonna make you so much a better programmer you will feel much more secure with the quality of your work and it will be a lot better and you will be more sought after. You will be able to write your programs with fewer bugs in less time. Your code will have fewer bugs because your code will be easier to reason about and you will be able to write it in less time because you will be able to reuse more of your code.

So let me tell you about higher-order functions because that is going to make me feel Super Smart.

In Kotlin and in all functional programming languages functions are values. Let me show you what I mean by that. Here we will see the basic function in its natural habitat.

fun triple(x: Int) : Int{
return x*3
}

So you will recognize this even if you don’t know Kotlin because all other programming languages have functions, but not all programming languages can do like below code, create an anonymous function and assign it to a variable just like any other value.

var triple = fun (x: Int) : Int{
return x*3
}

var waffle = triple

waffle(30)

You know? … Yes this will be 90

Again! in functional programming languages functions are values just like strings or numbers functions can be assigned to variables or pass into other functions, higher-order functions, but what are higher-order functions good for?… composition. The fact that we can take one function and put it into another function allows us to compose a lot of small functions into bigger functions.

That’s all a lot of theory. Let’s look at how to actually use one of these things. Probably the most basic and useful higher-order function is filter, It’s really simple. Filter is a function on the array that accepts another function as its argument which it will use to return a new filtered version of the array.

Here we see a list of animals and we want to filter out dogs.

I am going to show you how to do this using filter, but before I do I want to remind you how you do this with a normal for Loop.

var dogs = arrayListOf<Animals>()
for (animal in animals) {
if (animal.species=="dog")
dogs.add(animal)
}

So this is just an ultra normal for loop it creates an ArrayList to hold the dogs. It iterates over the animal ArrayList and if the species of the animal is being iterated is equal to dog it will add that dog onto the animals ArrayList.

Now let’s rewrite this using the filter function.

var dogs = animals.filter(fun(animal): Boolean {
return animal.species == "dog"
})

So let’s have a look at this, filter accepts one argument another function. Functions that you send into other functions are called Callback functions because the host function will call back to them. Filter will loop through each item in the array and for each item it’s going to pass it into the callback function and when it does it will expect the callback function to return either true, or false to tell filter whether or not this item should be in the new array and after it’s done, It will return the new filtered array, and that will be dogs.

Remember how I said that You’ll write software faster when you’re doing functional programming. Notice here that the example that uses filter is a lot less code than the for loop and That is not because the syntax is cosmetically shorter or anything it’s because we’re actually writing less code, less logic.

The reason that we need less logic is that when we write our software in small simple functions. They compose together which allows us to reuse functions all over the place. So in the filter example. We are just writing one line of logic really. The callback function and the filter function just slopped into each other. They are Composable. I’d really like to stress how well these simple functions compose.

Let’s break out the callback into a separate variable.

var isDog = fun(animal: Animals): Boolean {
return animal.species == "dog"
}
var dogs = animals.filter(isDog)

So notice here that is the dog is just a function that checks that an object is a dog. It doesn’t really have anything to do with the filtering at all. It’s completely decoupled from it, so we can use it for other things. For instance, if we wanted the animals that aren’t dogs, we could pass it to filterNot which is another a higher-order function on the array object that does the inverse of the filter so it could be animals that aren’t dogs.

Let’s see how this looks.

var otherAnimals = animals.filterNot(isDog)

You see that we very cleanly broken the problem up into two completely separate problems, the problem of determining if an animal is a dog or not, and the problem of creating an array and stuffing the objects into them, and this way we’re allowed to think and reason and debug these problems separately and that is a lot easier than when the two solutions are all jumbled together like in the for loop.

Today, I’ve talked about how functional programming means that you can write software with fewer bugs in less time. In Kotlin, functions are values and you can exploit this by dividing your code into small simple functions and composing them together using higher-order functions. As an example, I showed you the higher-order function filter and how it compares to normal for loop.

We’re just scratching the surface here. Learning functional programming is going to introduce you to a whole new world that you didn’t know existed. There are many useful higher-order functions besides filter next time I will talk about two of the map and reduce.

--

--

Sharjeel Haider
MindOrks

Android Engineer | React Native | Full stack developer for fun | Email: sharjeelhaidder@gmail.com