Working with Higher-Order Functions

Tutorial 2 of 5

Working with Higher-Order Functions in Kotlin

1. Introduction

Goal of the Tutorial

This tutorial aims to guide you on how to utilize higher-order functions in Kotlin programming.

Learning Objectives

By the end of this tutorial:
- You will understand the concept of higher-order functions.
- You will learn how to declare and utilize higher-order functions in your Kotlin programs.

Prerequisites

Basic knowledge of Kotlin programming is required. Familiarity with functions in Kotlin will be beneficial.

2. Step-by-Step Guide

What are Higher-Order Functions?

In Kotlin, higher-order functions are functions that can receive other functions as parameters or return a function. This feature allows for a more functional programming style.

How to Declare Higher-Order Functions

In Kotlin, you can declare a higher-order function in the following way:

fun <T> List<T>.customFilter(predicate: (T) -> Boolean): List<T> {
    val result = mutableListOf<T>()
    for (item in this) {
        if (predicate(item)) {
            result.add(item)
        }
    }
    return result
}

In the above example, customFilter() is a higher-order function because it takes a function predicate as a parameter.

3. Code Examples

Example 1

Let's take a look at a simple example of a higher-order function:

fun operation(): (Int) -> Int {
    return ::square
}

fun square(x: Int) = x * x

fun main() {
    val func = operation()
    println(func(2))  // Output: 4
}

Here, operation() is a higher-order function that returns another function square(). square() takes an integer as input and returns its square.

Example 2

Now, let's see an example where a higher-order function takes a function as a parameter:

fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
    return operation(a, b)
}

fun sum(x: Int, y: Int) = x + y

fun main() {
    val result = calculate(2, 3, ::sum)
    println(result)  // Output: 5
}

In this example, calculate() is a higher-order function, and sum() is a function passed as a parameter to calculate().

4. Summary

In this tutorial, we have learned about higher-order functions in Kotlin. We have also seen how to declare and utilize them in our Kotlin programs, which can be very beneficial for writing more functional and concise code.

5. Practice Exercises

Exercise 1

Write a higher-order function that takes a function as a parameter and applies it to two given integers.

Exercise 2

Write a higher-order function that returns a function that multiplies an input integer by a given factor.

Solutions

Solution to Exercise 1

fun applyOperation(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
    return operation(a, b)
}

fun subtraction(x: Int, y: Int) = x - y

fun main() {
    val result = applyOperation(10, 5, ::subtraction)
    println(result)  // Output: 5
}

Solution to Exercise 2

fun multiplyBy(factor: Int): (Int) -> Int {
    return { number -> number * factor }
}

fun main() {
    val multiplyBy2 = multiplyBy(2)
    println(multiplyBy2(4))  // Output: 8
}

Keep practicing to get more familiar with higher-order functions. Happy coding!