added simpler take() implementation
This commit is contained in:
@@ -155,11 +155,8 @@ public inline fun <in T> java.util.Iterator<T?>.requireNoNulls(): java.util.Iter
|
|||||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt takeExtractsTheFirstNElements
|
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt takeExtractsTheFirstNElements
|
||||||
*/
|
*/
|
||||||
public inline fun <T> java.util.Iterator<T>.take(n: Int): java.util.Iterator<T> {
|
public inline fun <T> java.util.Iterator<T>.take(n: Int): java.util.Iterator<T> {
|
||||||
fun countTo(n: Int): (T) -> Boolean {
|
var count = n
|
||||||
var count = 0
|
return takeWhile{ --count >= 0 }
|
||||||
return { ++count; count <= n }
|
|
||||||
}
|
|
||||||
return takeWhile(countTo(n))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user