W3cubDocs

/Kotlin

takeWhile

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> Sequence<T>.takeWhile(
    predicate: (T) -> Boolean
): Sequence<T>

Returns a sequence containing first elements satisfying the given predicate.

The operation is intermediate and stateless.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val chars = ('a'..'z').toList()
println(chars.take(3)) // [a, b, c]
println(chars.takeWhile { it < 'f' }) // [a, b, c, d, e]
println(chars.takeLast(2)) // [y, z]
println(chars.takeLastWhile { it > 'w' }) // [x, y, z]
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/take-while.html