Make stream plus operation actually streaming (lazy).
This commit is contained in:
committed by
Andrey Breslav
parent
c5b0351aa7
commit
108fdc0097
@@ -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,6 +1,5 @@
|
||||
package kotlin.dom
|
||||
|
||||
import kotlin.*
|
||||
import kotlin.support.*
|
||||
import java.util.*
|
||||
import org.w3c.dom.*
|
||||
|
||||
@@ -16,6 +16,7 @@ fun generateCollectionsAPI(outDir : File) {
|
||||
guards().writeTo(File(outDir, "_Guards.kt")) { build() }
|
||||
generators().writeTo(File(outDir, "_Generators.kt")) { build() }
|
||||
strings().writeTo(File(outDir, "_Strings.kt")) { build() }
|
||||
streams().writeTo(File(outDir, "_Streams.kt")) { build() }
|
||||
specialJVM().writeTo(File(outDir, "_SpecialJVM.kt")) { build() }
|
||||
|
||||
numeric().writeTo(File(outDir, "_Numeric.kt")) {
|
||||
|
||||
@@ -18,12 +18,9 @@ fun generators(): List<GenericFunction> {
|
||||
|
||||
doc(Streams) { "Returns a stream containing all elements of original stream and then the given element" }
|
||||
returns(Streams) { "Stream<T>" }
|
||||
// TODO: Implement lazy behavior
|
||||
body(Streams) {
|
||||
"""
|
||||
val answer = toArrayList()
|
||||
answer.add(element)
|
||||
return answer.stream()
|
||||
return Multistream(streamOf(this, streamOf(element)))
|
||||
"""
|
||||
}
|
||||
}
|
||||
@@ -32,7 +29,6 @@ fun generators(): List<GenericFunction> {
|
||||
exclude(Streams)
|
||||
doc { "Returns a list containing all elements of original collection and then all elements of the given *collection*" }
|
||||
returns("List<T>")
|
||||
|
||||
body {
|
||||
"""
|
||||
val answer = toArrayList()
|
||||
@@ -46,7 +42,6 @@ fun generators(): List<GenericFunction> {
|
||||
exclude(Streams)
|
||||
doc { "Returns a list containing all elements of original collection and then all elements of the given *collection*" }
|
||||
returns("List<T>")
|
||||
|
||||
body {
|
||||
"""
|
||||
val answer = toArrayList()
|
||||
@@ -60,13 +55,9 @@ fun generators(): List<GenericFunction> {
|
||||
only(Streams)
|
||||
doc { "Returns a stream containing all elements of original stream and then all elements of the given *collection*" }
|
||||
returns("Stream<T>")
|
||||
|
||||
// TODO: Implement lazy behavior
|
||||
body {
|
||||
"""
|
||||
val answer = toArrayList()
|
||||
answer.addAll(collection)
|
||||
return answer.stream()
|
||||
return Multistream(streamOf(this, collection.stream()))
|
||||
"""
|
||||
}
|
||||
}
|
||||
@@ -76,11 +67,8 @@ fun generators(): List<GenericFunction> {
|
||||
doc { "Returns a stream containing all elements of original stream and then all elements of the given *stream*" }
|
||||
returns("Stream<T>")
|
||||
body {
|
||||
// TODO: Implement lazy behavior
|
||||
"""
|
||||
val answer = toArrayList()
|
||||
answer.addAll(stream)
|
||||
return answer.stream()
|
||||
return Multistream(streamOf(this, stream))
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package templates
|
||||
|
||||
import templates.Family.*
|
||||
|
||||
fun streams(): List<GenericFunction> {
|
||||
val templates = arrayListOf<GenericFunction>()
|
||||
|
||||
templates add f("stream()") {
|
||||
doc { "Returns a stream from the given collection" }
|
||||
returns("Stream<T>")
|
||||
body {
|
||||
"""
|
||||
return object : Stream<T> { override fun iterator() : Iterator<T> { return this@stream.iterator() } }
|
||||
"""
|
||||
}
|
||||
|
||||
body(Streams) {
|
||||
"""
|
||||
return this
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
return templates
|
||||
}
|
||||
Reference in New Issue
Block a user