Skip to content

Commit

Permalink
Claim a stream produced from a Sequence having ORDERED property.
Browse files Browse the repository at this point in the history
Add a test verifying that property is respected.
  • Loading branch information
ilya-g committed Jul 1, 2016
1 parent 22ee3a8 commit 157ed3f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libraries/stdlib/jre8/src/kotlin/streams/Streams.kt
Expand Up @@ -27,7 +27,7 @@ public fun DoubleStream.asSequence(): Sequence<Double> = Sequence { iterator() }
/**
* Creates a sequential [Stream] instance that produces elements from the original sequence.
*/
public fun <T> Sequence<T>.asStream(): Stream<T> = StreamSupport.stream({ Spliterators.spliteratorUnknownSize(iterator(), 0) }, 0, false)
public fun <T> Sequence<T>.asStream(): Stream<T> = StreamSupport.stream({ Spliterators.spliteratorUnknownSize(iterator(), Spliterator.ORDERED) }, Spliterator.ORDERED, false)

/**
* Returns a [List] containing all elements produced by this stream.
Expand Down
8 changes: 8 additions & 0 deletions libraries/stdlib/jre8/test/collections/StreamsTest.kt
Expand Up @@ -41,6 +41,14 @@ class StreamsTest {
assertEquals(expected.limit(7).toList(), stream.limit(7).toList())
}

@Test fun asParallelStream() {
val sequence = generateSequence(0) { it + 2 } // even numbers
val stream = sequence.asStream().parallel().map { it / 2 }

val n = 100000
val expected = (0 until n).toList()

assertEquals(expected, stream.limit(n.toLong()).toList())
}

}

0 comments on commit 157ed3f

Please sign in to comment.