Server Side Kotlin-08

Server Side Kotlin-08

·

1 min read

lambda functions that take a collection element as a input and return a boolean value is called Predicate. In Kotlin, filtering conditions uses predicates. These functions do not change the original collection , and available for both mutable and read-only collections.chaining of function after filtering is also possible.

val games = listOf("unity", "unreal", "godot", "bevy")
val filterbyfour = games.filter {it.length>4}
println(filterbyfour)

The predicates in filter() can only check the values of the elements, however filterIndexed() takes predicate with two arguments: the index itself and the value of an element.

val games = listOf("unity", "unreal", "godot", "bevy")
val filteredIdx = games.filterIndexed { index, t -> (index != 1) && (t.length > 4)  }
println(filteredIdx)

output
[unity, godot]