map, filter, reduce
In this page:
val mapped = list.map { it transformation }
val filtered = list.filter { condition }
val total = list.reduce { acc, item -> acc + item }
Transforming with map
.map { } applies a transformation to every element of a collection and returns a brand-new list with the results, leaving the original collection untouched.
Example: Transforming with map
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4)`
val numbers = listOf(1, 2, 3, 4)
// Declare a read-only variable `squares`, set to `numbers.map { it * it }`
val squares = numbers.map { it * it }
// Print `squares` to the console, with a trailing newline
println(squares)
}
Login to try C/C++/Java/PHP code in the editor
Selecting with filter
.filter { } returns a new list containing only the elements for which the given predicate returns true, discarding the rest.
Example: Selecting with filter
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5, 6)`
val numbers = listOf(1, 2, 3, 4, 5, 6)
// Declare a read-only variable `evens`, set to `numbers.filter { it % 2 == 0 }`
val evens = numbers.filter { it % 2 == 0 }
// Print `evens` to the console, with a trailing newline
println(evens)
}
Login to try C/C++/Java/PHP code in the editor
Combining into One Value with reduce
.reduce { acc, item -> ... } repeatedly combines elements into a single accumulated result, starting with the first element as the initial accumulator value.
Example: Combining into One Value with reduce
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4)`
val numbers = listOf(1, 2, 3, 4)
// Declare a read-only variable `sum`, set to `numbers.reduce { acc, n -> acc + n }`
val sum = numbers.reduce { acc, n -> acc + n }
// Print "Sum via reduce: $sum" to the console, with a trailing newline
println("Sum via reduce: $sum")
}
Login to try C/C++/Java/PHP code in the editor
Combining with fold and an Initial Value
.fold(initial) { acc, item -> ... } behaves like reduce but starts from an explicit initial value, which also makes it safe to use on an empty collection.
Example: Combining with fold and an Initial Value
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf<Int>()`
val numbers = listOf<Int>()
// Declare a read-only variable `sum`, set to `numbers.fold(0) { acc, n -> acc + n }`
val sum = numbers.fold(0) { acc, n -> acc + n }
// Print "Sum via fold on empty list: $sum" to the console, with a trailing newline
println("Sum via fold on empty list: $sum")
// Declare a read-only variable `words`, set to `listOf("Kotlin", "is", "fun")`
val words = listOf("Kotlin", "is", "fun")
// Declare a read-only variable `sentence`, set to `words.fold("Result:") { acc, word -> "$acc $word" }`
val sentence = words.fold("Result:") { acc, word -> "$acc $word" }
// Print `sentence` to the console, with a trailing newline
println(sentence)
}
Login to try C/C++/Java/PHP code in the editor
- Confusing
.reduce { }with.fold(initial) { };reduceuses the first element as the starting accumulator and throws on an empty collection, whilefoldtakes an explicit initial value. - Chaining
.filter { }.map { }in the wrong order and getting different (though sometimes equally valid) results than intended. - Forgetting these functions return a new collection or value and never mutate the original collection.
.map { transform }returns a new list with each element transformed according to the given function..filter { predicate }returns a new list containing only the elements that satisfy the given condition..reduce { acc, item -> ... }combines all elements into a single result, using the first element as the initial accumulator..fold(initial) { acc, item -> ... }is likereducebut takes an explicit starting value, so it also works on empty collections.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: