takeWhile

inline fun <T> Array<out T>.takeWhile(predicate: (T) -> Boolean): List<T>(source)
inline fun ByteArray.takeWhile(predicate: (Byte) -> Boolean): List<Byte>(source)
inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean): List<Short>(source)
inline fun IntArray.takeWhile(predicate: (Int) -> Boolean): List<Int>(source)
inline fun LongArray.takeWhile(predicate: (Long) -> Boolean): List<Long>(source)
inline fun FloatArray.takeWhile(predicate: (Float) -> Boolean): List<Float>(source)
inline fun DoubleArray.takeWhile(predicate: (Double) -> Boolean): List<Double>(source)
inline fun BooleanArray.takeWhile(predicate: (Boolean) -> Boolean): List<Boolean>(source)
inline fun CharArray.takeWhile(predicate: (Char) -> Boolean): List<Char>(source)
inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T>(source)

Returns a list containing first elements satisfying the given predicate.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //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
}