A sequence constructed with initialValue can be iterated multiple times.

This commit is contained in:
Ilya Gorbunov
2015-03-27 17:42:05 +03:00
parent 3de0dff575
commit 698ffa2807
2 changed files with 48 additions and 4 deletions
@@ -176,7 +176,9 @@ public class SequenceTest {
test fun streamFromFunctionWithInitialValue() {
val values = sequence(3) { n -> if (n > 0) n - 1 else null }
assertEquals(listOf(3, 2, 1, 0), values.toList())
val expected = listOf(3, 2, 1, 0)
assertEquals(expected, values.toList())
assertEquals(expected, values.toList(), "Iterating sequence second time yields the same result")
}
private fun <T, C : MutableCollection<in T>> Sequence<T>.takeWhileTo(result: C, predicate: (T) -> Boolean): C {