filter
public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
return filterTo(ArrayList<T>(), predicate)
}
public inline fun <T, C: MutableCollection<in T>>
Iterable<T>.filterTo(destination: C, predicate: (T) -> Boolean): C {
for (element in this) if (predicate(element)) destination.add(element)
return destination
}
val list = listOf(1, 2, 3, 4, 5)
println(list.filter{ it > 3 }) // [4, 5]
dropWhile 과 비슷하지만 dropWhile 은 false 를 만나는 순간 리턴하지만, filter 는 주어진 개수를 끝까지 돈다
순서가 보장되어 있다면 dropWhile 을 사용하는 편이 조금더 효율이 좋다 할 수 있겠
순서보장이 되지 않는다면 어쩔수 없이 filter 를 사용한다
Last updated