Kotlin Functions Every Developer Should Know with Examples

Duggu
3 min readOct 21, 2023

Certainly! Here’s a collection of essential Kotlin functions that every developer should know, along with examples for each function:

let:

  • Use let to execute a block of code on a non-null object.
val name: String? = "John" 
name?.let {
println("Name is not null: $it")
}

run:

  • Use run to execute a block of code on an object, and it returns the result of the last expression.
val result = "Kotlin".run {
println("The length is: $length")
length * 2
}

with:

  • Use with to work with an object without the need to prefix each property or method with the object's name.
val person = Person("Alice", 30) with(person) {
println("Name: $name, Age: $age")
}

apply:

  • Use apply to configure an object's properties and return the object itself.
val person = Person() person.apply {
name = "Bob"
age = 25
}

also:

  • Use also to perform an additional action on an object and return the object itself.
val list = mutableListOf(1, 2, 3) 
val newList = list.also { it.add(4) }

takeIf:

  • Use takeIf to check a condition on an object and return it if the condition is met, or null otherwise.
val number = 7 
val result = number.takeIf { it % 2 == 0 }

takeUnless:

  • Similar to takeIf, but it returns the object if the condition is not met.
val number = 7 
val result = number.takeUnless { it % 2 == 0 }

map:

  • Use map to transform elements in a collection and return a new collection with the transformed elements.
val numbers = listOf(1, 2, 3, 4, 5) 
val squared = numbers.map { it * it }

filter:

  • Use filter to filter elements in a collection based on a condition and return a new collection with the filtered elements.
val numbers = listOf(1, 2, 3, 4, 5) 
val evenNumbers = numbers.filter { it % 2 == 0 }

reduce:

  • Use reduce to perform a cumulative operation on the elements of a collection.
val numbers = listOf(1, 2, 3, 4, 5) 
val sum = numbers.reduce { acc, number -> acc + number }

forEach:

  • Use forEach to iterate through the elements of a collection and perform an action on each element.
val fruits = listOf("apple", "banana", "cherry")
fruits.forEach { println("Fruit: $it") }

when expression:

  • Use the when expression for conditional branching.
val grade = 85 val result = when {
grade >= 90 -> "A"
grade >= 80 -> "B"
grade >= 70 -> "C"
else -> "F"
}

These are just a few essential Kotlin functions and features every developer should know. Kotlin offers a wide range of powerful functions and features that can help make your code more concise and expressive.

Perhaps my article will offer some clarity. If you find it valuable, please consider following and applauding. Your support will inspire me to write more articles like this.

#kotlin #kotlindeveloper #android #androiddeveloper #softwaredevelopment #androiddevelopment #kotlinandroid #developers #developercommunity #softwareengineer #googledevs #googlegroups #androidgroups #googleandroid #gradle #gradleandroid #community #linkedinforcreators #creators #linkedin #linkedinlearning #linkedinfamily #linkedinconnections #lamda

--

--