Functional Programming in Kotlin: Principles and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Functional Programming in Kotlin



Kotlin is a modern multi-paradigm language that seamlessly combines object-oriented and functional programming. The functional style allows you to write more predictable, concise, and testable code. In this article, we will break down the key concepts of functional programming (FP) in Kotlin: pure functions, data immutability, higher-order functions, and working with collections through map, filter, and fold. The material is intended for developers already familiar with the basics of Kotlin syntax.



Pure Functions and Immutability



What is a Pure Function?

A pure function is a function that:

  • Always returns the same result for the same arguments (deterministic).
  • Has no side effects (does not modify external state, does not read global variables, does not perform input/output).


Example of a pure function:

fun sum(a: Int, b: Int): Int = a + b


Example of an impure function (depends on external state):

var counter = 0fun impureSum(a: Int, b: Int): Int {    counter++ // side effect    return a + b}


Immutability

In FP, it is common practice to avoid changing data after it is created. In Kotlin, the following are used for this:

  • val instead of var for declaring immutable references.
  • Immutable collections: listOf, mapOf, setOf.
  • Copying objects with modifications via copy() for data classes.


data class User(val name: String, val age: Int)

fun main() { val user = User("Alice", 25) val updatedUser = user.copy(age = 26) // creating a new copy println(user) // User(name=Alice, age=25) println(updatedUser) // User(name=Alice, age=26)}


Advantages of immutability: simplified debugging, thread safety, code predictability.



Higher-Order Functions and Lambda Expressions



Functions as Values

In Kotlin, functions can be passed as arguments, returned from other functions, and stored in variables. This is the foundation of the functional style.



// Function that takes another functionfun operateOnNumbers(a: Int, b: Int, operation: (Int, Int) -> Int): Int {    return operation(a, b)}

fun main() { val sum = operateOnNumbers(5, 3) { x, y -> x + y } val product = operateOnNumbers(5, 3) { x, y -> x * y } println("Sum: $sum, Product: $product")}


Lambda Expressions

A lambda is an anonymous function written in a compact form. Syntax: { arguments -> body }.



val square: (Int) -> Int = { x -> x * x }val result = square(4) // 16

// If there is one argument, you can use the implicit name itval double: (Int) -> Int = { it * 2 }


Built-in Higher-Order Functions

Kotlin provides many useful functions for working with collections:

  • map — transforms each element.
  • filter — keeps elements that satisfy a condition.
  • fold — reduces a collection to a single value with an initial accumulator.


fun main() {    val numbers = listOf(1, 2, 3, 4, 5)

// map: square each element val squares = numbers.map { it * it } println(squares) // [1, 4, 9, 16, 25]

// filter: keep only even numbers val evens = numbers.filter { it % 2 == 0 } println(evens) // [2, 4]

// fold: sum of all elements val sum = numbers.fold(0) { acc, element -> acc + element } println(sum) // 15

// Method chaining: even squares val evenSquares = numbers .filter { it %

Blogs

Book Recommendations