https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

sitepodmatt

03/10/2019, 6:23 AM
Copy code
fun <T> Iterable<T>.filterUntil(predicate : (T) -> Boolean) : Sequence<T> {
    return sequence<T> {
        val iter = iterator()
        while (iter.hasNext()) {
            val n = iter.next()
            if (predicate(n)) {
                break
            }
            yield(n)
        }
    }
}
fun main(argv: Array<String>) {
    listOf(1,2,3,4,5).filterUntil { it == 3 }.map { it * 2}.forEach {
        println(it)
    }

}