Make stream plus operation actually streaming (lazy).

This commit is contained in:
Ilya Ryzhenkov
2014-03-05 19:53:53 +04:00
committed by Andrey Breslav
parent c5b0351aa7
commit 108fdc0097
8 changed files with 158 additions and 30 deletions
@@ -420,9 +420,7 @@ public fun <T> Iterable<T>.plus(collection: Iterable<T>) : List<T> {
* Returns a stream containing all elements of original stream and then all elements of the given *collection*
*/
public fun <T> Stream<T>.plus(collection: Iterable<T>) : Stream<T> {
val answer = toArrayList()
answer.addAll(collection)
return answer.stream()
return Multistream(streamOf(this, collection.stream()))
}
@@ -530,9 +528,7 @@ public fun <T> Iterable<T>.plus(element: T) : List<T> {
* Returns a stream containing all elements of original stream and then the given element
*/
public fun <T> Stream<T>.plus(element: T) : Stream<T> {
val answer = toArrayList()
answer.add(element)
return answer.stream()
return Multistream(streamOf(this, streamOf(element)))
}
@@ -540,9 +536,7 @@ public fun <T> Stream<T>.plus(element: T) : Stream<T> {
* Returns a stream containing all elements of original stream and then all elements of the given *stream*
*/
public fun <T> Stream<T>.plus(stream: Stream<T>) : Stream<T> {
val answer = toArrayList()
answer.addAll(stream)
return answer.stream()
return Multistream(streamOf(this, stream))
}
@@ -0,0 +1,96 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import java.util.*
/**
* Returns a stream from the given collection
*/
public fun <T> Array<out T>.stream() : Stream<T> {
return object : Stream<T> { override fun iterator() : Iterator<T> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun BooleanArray.stream() : Stream<Boolean> {
return object : Stream<Boolean> { override fun iterator() : Iterator<Boolean> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun ByteArray.stream() : Stream<Byte> {
return object : Stream<Byte> { override fun iterator() : Iterator<Byte> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun CharArray.stream() : Stream<Char> {
return object : Stream<Char> { override fun iterator() : Iterator<Char> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun DoubleArray.stream() : Stream<Double> {
return object : Stream<Double> { override fun iterator() : Iterator<Double> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun FloatArray.stream() : Stream<Float> {
return object : Stream<Float> { override fun iterator() : Iterator<Float> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun IntArray.stream() : Stream<Int> {
return object : Stream<Int> { override fun iterator() : Iterator<Int> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun LongArray.stream() : Stream<Long> {
return object : Stream<Long> { override fun iterator() : Iterator<Long> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun ShortArray.stream() : Stream<Short> {
return object : Stream<Short> { override fun iterator() : Iterator<Short> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun <T> Iterable<T>.stream() : Stream<T> {
return object : Stream<T> { override fun iterator() : Iterator<T> { return this@stream.iterator() } }
}
/**
* Returns a stream from the given collection
*/
public fun <T> Stream<T>.stream() : Stream<T> {
return this
}
@@ -6,11 +6,7 @@ public trait Stream<out T> {
public fun iterator(): Iterator<T>
}
public fun <T> Iterable<T>.stream(): Stream<T> = object : Stream<T> {
override fun iterator(): Iterator<T> {
return this@stream.iterator()
}
}
public fun <T> streamOf(vararg elements : T) : Stream<T> = elements.stream()
public class FilteringStream<T>(val stream: Stream<T>, val sendWhen: Boolean = true, val predicate: (T) -> Boolean) : Stream<T> {
override fun iterator(): Iterator<T> = object : AbstractIterator<T>() {
@@ -84,6 +80,35 @@ public class FlatteningStream<T, R>(val stream: Stream<T>, val transformer: (T)
}
}
public class Multistream<T>(val streams: Stream<Stream<T>>) : Stream<T> {
override fun iterator(): Iterator<T> = object : AbstractIterator<T>() {
val iterator = streams.iterator()
var streamIterator: Iterator<T>? = null
override fun computeNext() {
while (streamIterator == null) {
if (!iterator.hasNext()) {
done()
break;
} else {
val stream = iterator.next()
val nextStreamIterator = stream.iterator()
if (nextStreamIterator.hasNext())
streamIterator = nextStreamIterator
}
}
val currentStreamIterator = streamIterator
if (currentStreamIterator == null) {
done()
} else {
setNext(currentStreamIterator.next())
if (!currentStreamIterator.hasNext())
streamIterator = null
}
}
}
}
public class LimitedStream<T>(val stream: Stream<T>, val stopWhen: Boolean = true, val predicate: (T) -> Boolean) : Stream<T> {
override fun iterator(): Iterator<T> = object : AbstractIterator<T>() {
val iterator = stream.iterator()
-1
View File
@@ -1,6 +1,5 @@
package kotlin.dom
import kotlin.*
import kotlin.support.*
import java.util.*
import org.w3c.dom.*