Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
In Kotlin, functions are first-class citizens. This means you can store them in variables, pass them as parameters, and return them from other functions. A function that takes another function as a parameter or returns a function is called a higher-order function.
Here's a tutorial on higher-order functions in Kotlin:
A simple higher-order function that accepts a function as a parameter:
fun calculate(x: Int, y: Int, operation: (Int, Int) -> Int): Int { return operation(x, y) } val sum = calculate(3, 4, { a, b -> a + b }) // Result: 7
In the above example, (Int, Int) -> Int
is a function type that represents a function taking two Int
parameters and returning an Int
.
A higher-order function can also return another function:
fun multiplyBy(factor: Int): (Int) -> Int { return { number -> number * factor } } val double = multiplyBy(2) println(double(4)) // Result: 8
In Kotlin, a lambda is a short way of representing a function. In the first example, { a, b -> a + b }
is a lambda expression.
When the lambda has only one parameter, you can use the it
keyword:
val numbers = listOf(1, 2, 3, 4) numbers.filter { it % 2 == 0 } // Filters even numbers
Kotlin's standard library provides several higher-order functions like map
, filter
, forEach
, etc.:
val doubled = numbers.map { it * 2 } val even = numbers.filter { it % 2 == 0 }
Using higher-order functions can introduce runtime overhead because of the creation of function objects. To mitigate this, Kotlin offers inline
functions:
inline fun performOperation(x: Int, y: Int, op: (Int, Int) -> Int): Int { return op(x, y) }
Using the inline
keyword instructs the compiler to copy the lambda's code to the calling place, reducing the overhead.
Lambdas can modify variables from the outer scope:
var sum = 0 numbers.forEach { sum += it }
You can also pass named functions as arguments if they match the expected type:
fun sum(a: Int, b: Int) = a + b val result = calculate(5, 3, ::sum) // Using :: to reference the function
You can use extension functions as lambdas:
val stringList = listOf("Kotlin", "Java", "C++") val sortedList = stringList.sortedWith(Comparator<String>::length)
Higher-order functions and lambdas are powerful features of Kotlin. They allow for concise and expressive code, especially when working with collections. They're essential for functional programming styles in Kotlin and provide a flexible way to encapsulate behavior. Familiarizing yourself with these concepts can help in writing clean and efficient Kotlin code.
Passing functions as parameters in Kotlin:
fun operation(x: Int, y: Int, op: (Int, Int) -> Int): Int { return op(x, y) }
Returning functions from functions in Kotlin:
fun multiplyBy(factor: Int): (Int) -> Int { return { number -> number * factor } }
Anonymous functions and lambda expressions in Kotlin:
val square: (Int) -> Int = { x -> x * x } val add: (Int, Int) -> Int = { a, b -> a + b }
Function types in Kotlin:
val operation: (Int, Int) -> Int = { x, y -> x + y }
Using higher-order functions with collections in Kotlin:
val numbers = listOf(1, 2, 3, 4, 5) val squared = numbers.map { it * it }
Higher-order functions vs regular functions in Kotlin:
fun add(x: Int, y: Int): Int { return x + y } val higherOrderAdd: (Int, Int) -> Int = { a, b -> a + b }
Function references in Kotlin:
::
syntax.val sum: (Int, Int) -> Int = ::add
Examples of higher-order functions in Kotlin:
val numbers = listOf(1, 2, 3, 4, 5) val sum = numbers.reduce { acc, value -> acc + value }
Functional programming principles in Kotlin:
val squaredNumbers = numbers.map { it * it }
Built-in higher-order functions in Kotlin standard library:
val filteredNumbers = numbers.filter { it % 2 == 0 }
Higher-order extension functions in Kotlin:
fun List<Int>.customSum(): Int { return this.reduce { acc, value -> acc + value } }