added simpler take() implementation

This commit is contained in:
James Strachan
2012-04-04 19:40:40 +01:00
parent b4cf0bff96
commit 0b813e131f
+2 -5
View File
@@ -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))
} }
/** /**