Kotlin functional programming

Kotlin functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. In functional programming, functions are first-class citizens, meaning they can be passed as arguments to other functions, returned as values from other functions, and assigned to variables.

Key Concepts in Functional Programming:

  1. Immutability: Data, once created, cannot be changed. Instead of modifying existing data, new data structures are created.
  2. Pure Functions: Functions that always return the same output for the same input and have no side effects.
  3. Higher-Order Functions: Functions that take other functions as parameters or return them as results.
  4. Referential Transparency: The ability to replace a function call with its resulting value without changing the program’s behavior.

Syntax in Kotlin:

Kotlin is a modern programming language that fully supports functional programming paradigms. Here’s a quick overview of some key syntax used in Kotlin for functional programming.

Lambda Expressions:

Kotlin
val square: (Int) -> Int = { x -> x * x }
println(square(5)) // Output: 25

Output:

Kotlin
25

Higher-Order Functions:

Kotlin
fun applyOperation(x: Int, operation: (Int) -> Int): Int {
    return operation(x)
}

val result = applyOperation(10) { it * it }
println(result) // Output: 100

Output:

Kotlin
100

Filtering and Mapping:

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter { it % 2 == 0 }
println(evenNumbers) // Output: [2, 4]

val squaredNumbers = numbers.map { it * it }
println(squaredNumbers) // Output: [1, 4, 9, 16, 25]

Output:

Kotlin
[2, 4]
[1, 4, 9, 16, 25]

Functional Composition:

Kotlin
fun square(x: Int) = x * x
fun double(x: Int) = x * 2

val squareOfDouble = square compose double
println(squareOfDouble(5)) // Output: 100

Output:

Kotlin
100

Sequences:

Kotlin
val evenSquares = generateSequence(1) { it + 1 }
    .map { it * it }
    .filter { it % 2 == 0 }
    .take(5)
    .toList()

println(evenSquares) // Output: [4, 16, 36, 64, 100]

Output:

Kotlin
[4, 16, 36, 64, 100]

Immutable Collections:

Kotlin
val immutableList = listOf(1, 2, 3, 4, 5)
val immutableMap = mapOf("a" to 1, "b" to 2)

// Attempting to modify these collections will result in compilation errors

No explicit output is provided since this code snippet demonstrates immutability, which prevents modification of collections.

Real-World Example: Calculating Factorials

Let’s see a real-world example of functional programming in Kotlin by calculating factorials using recursion:

Kotlin
fun factorial(n: Int): Int {
    return if (n <= 1) 1 else n * factorial(n - 1)
}

println(factorial(5)) // Output: 120

Output:

Kotlin
120
Functional programming in Kotlin enables developers to write concise, expressive, and maintainable code by leveraging higher-order functions, lambda expressions, and immutable data structures. This approach promotes code reusability, modularity, and readability, making Kotlin a versatile choice for modern software development projects.