Deprecate sequence implementations in favor of sequence operations.

This commit is contained in:
Ilya Gorbunov
2015-04-15 20:05:47 +03:00
parent b2b98502e9
commit 58310d172f
10 changed files with 34 additions and 38 deletions
+2 -2
View File
@@ -579,7 +579,7 @@ public fun <T : Any> Iterable<T?>.filterNotNull(): List<T> {
* Returns a sequence containing all elements that are not null
*/
public fun <T : Any> Sequence<T?>.filterNotNull(): Sequence<T> {
return FilteringSequence(this, false, { it == null }) as Sequence<T>
return filterNot { it == null } as Sequence<T>
}
@@ -588,7 +588,7 @@ deprecated("Migrate to using Sequence<T> and respective functions")
* Returns a stream containing all elements that are not null
*/
public fun <T : Any> Stream<T?>.filterNotNull(): Stream<T> {
return FilteringStream(this, false, { it == null }) as Stream<T>
return filterNot { it == null } as Stream<T>
}
/**
@@ -705,7 +705,7 @@ public fun <T> Iterable<T>.plus(collection: Iterable<T>): List<T> {
* Returns a sequence containing all elements of original sequence and then all elements of the given [collection]
*/
public fun <T> Sequence<T>.plus(collection: Iterable<T>): Sequence<T> {
return MultiSequence(sequenceOf(this, collection.sequence()))
return sequenceOf(this, collection.sequence()).flatten()
}
@@ -714,7 +714,7 @@ deprecated("Migrate to using Sequence<T> and respective functions")
* 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> {
return Multistream(streamOf(this, collection.stream()))
return streamOf(this, collection.stream()).flatten()
}
/**
@@ -811,7 +811,7 @@ public fun <T> Iterable<T>.plus(element: T): List<T> {
* Returns a sequence containing all elements of original sequence and then the given element
*/
public fun <T> Sequence<T>.plus(element: T): Sequence<T> {
return MultiSequence(sequenceOf(this, sequenceOf(element)))
return sequenceOf(this, sequenceOf(element)).flatten()
}
@@ -820,14 +820,14 @@ deprecated("Migrate to using Sequence<T> and respective functions")
* Returns a stream containing all elements of original stream and then the given element
*/
public fun <T> Stream<T>.plus(element: T): Stream<T> {
return Multistream(streamOf(this, streamOf(element)))
return streamOf(this, streamOf(element)).flatten()
}
/**
* Returns a sequence containing all elements of original sequence and then all elements of the given [sequence]
*/
public fun <T> Sequence<T>.plus(sequence: Sequence<T>): Sequence<T> {
return MultiSequence(sequenceOf(this, sequence))
return sequenceOf(this, sequence).flatten()
}
@@ -836,7 +836,7 @@ deprecated("Migrate to using Sequence<T> and respective functions")
* 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> {
return Multistream(streamOf(this, stream))
return streamOf(this, stream).flatten()
}
/**
+2 -12
View File
@@ -49,12 +49,7 @@ public fun <T : Any> List<T?>.requireNoNulls(): List<T> {
* Returns an original collection containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
public fun <T : Any> Sequence<T?>.requireNoNulls(): Sequence<T> {
return FilteringSequence(this) {
if (it == null) {
throw IllegalArgumentException("null element found in $this")
}
true
} as Sequence<T>
return map { it ?: throw IllegalArgumentException("null element found in $this") }
}
@@ -63,11 +58,6 @@ deprecated("Migrate to using Sequence<T> and respective functions")
* Returns an original collection containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
public fun <T : Any> Stream<T?>.requireNoNulls(): Stream<T> {
return FilteringStream(this) {
if (it == null) {
throw IllegalArgumentException("null element found in $this")
}
true
} as Stream<T>
return map { it ?: throw IllegalArgumentException("null element found in $this") }
}
@@ -471,7 +471,7 @@ public inline fun <reified R> Iterable<*>.filterIsInstance(): List<R> {
* Returns a sequence containing all elements that are instances of specified type parameter R
*/
public inline fun <reified R> Sequence<*>.filterIsInstance(): Sequence<R> {
return FilteringSequence(this, true, { it is R }) as Sequence<R>
return filter { it is R } as Sequence<R>
}
@@ -480,7 +480,7 @@ deprecated("Migrate to using Sequence<T> and respective functions")
* Returns a stream containing all elements that are instances of specified type parameter R
*/
public inline fun <reified R> Stream<*>.filterIsInstance(): Stream<R> {
return FilteringStream(this, true, { it is R }) as Stream<R>
return filter { it is R } as Stream<R>
}
/**
@@ -501,7 +501,7 @@ public fun <R> Iterable<*>.filterIsInstance(klass: Class<R>): List<R> {
* Returns a sequence containing all elements that are instances of specified class
*/
public fun <R> Sequence<*>.filterIsInstance(klass: Class<R>): Sequence<R> {
return FilteringSequence(this, true, { klass.isInstance(it) }) as Sequence<R>
return filter { klass.isInstance(it) } as Sequence<R>
}
@@ -510,7 +510,7 @@ deprecated("Migrate to using Sequence<T> and respective functions")
* Returns a stream containing all elements that are instances of specified class
*/
public fun <R> Stream<*>.filterIsInstance(klass: Class<R>): Stream<R> {
return FilteringStream(this, true, { klass.isInstance(it) }) as Stream<R>
return filter { klass.isInstance(it) } as Stream<R>
}
/**
@@ -18,7 +18,7 @@ public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
* Returns a sequence of all elements from all sequences in this sequence.
*/
public fun <T> Sequence<Sequence<T>>.flatten(): Sequence<T> {
return FlatteningSequence(this, { it })
return MultiSequence(this)
}
/**
@@ -63,6 +63,7 @@ public class FilteringStream<T>(stream: Stream<T>, sendWhen: Boolean = true, pre
* @param sendWhen If `true`, values for which the predicate returns `true` are returned. Otherwise,
* values for which the predicate returns `false` are returned
*/
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.filter() or sequence.filterNot() instead.")
public class FilteringSequence<T>(private val sequence: Sequence<T>,
private val sendWhen: Boolean = true,
private val predicate: (T) -> Boolean
@@ -112,6 +113,7 @@ public class TransformingStream<T, R>(stream: Stream<T>, transformer: (T) -> R)
* A sequence which returns the results of applying the given [transformer] function to the values
* in the underlying [sequence].
*/
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.map() instead.")
public class TransformingSequence<T, R>(private val sequence: Sequence<T>, private val transformer: (T) -> R) : Sequence<R> {
override fun iterator(): Iterator<R> = object : Iterator<R> {
val iterator = sequence.iterator()
@@ -134,6 +136,7 @@ public class TransformingIndexedStream<T, R>(stream: Stream<T>, transformer: (In
* in the underlying [sequence], where the transformer function takes the index of the value in the underlying
* sequence along with the value itself.
*/
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.mapIndexed() instead.")
public class TransformingIndexedSequence<T, R>(private val sequence: Sequence<T>, private val transformer: (Int, T) -> R) : Sequence<R> {
override fun iterator(): Iterator<R> = object : Iterator<R> {
val iterator = sequence.iterator()
@@ -156,6 +159,7 @@ public class IndexingStream<T>(stream: Stream<T>)
* A sequence which combines values from the underlying [sequence] with their indices and returns them as
* [IndexedValue] objects.
*/
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.withIndex() instead.")
public class IndexingSequence<T>(private val sequence: Sequence<T>) : Sequence<IndexedValue<T>> {
override fun iterator(): Iterator<IndexedValue<T>> = object : Iterator<IndexedValue<T>> {
val iterator = sequence.iterator()
@@ -179,6 +183,7 @@ public class MergingStream<T1, T2, V>(stream1: Stream<T1>, stream2: Stream<T2>,
* [transform] function and returns the values returned by that function. The sequence stops returning
* values as soon as one of the underlying sequences stops returning values.
*/
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.merge() instead.")
public class MergingSequence<T1, T2, V>(private val sequence1: Sequence<T1>,
private val sequence2: Sequence<T2>,
private val transform: (T1, T2) -> V
@@ -200,6 +205,7 @@ deprecated("Use FlatteningSequence<T> instead")
public class FlatteningStream<T, R>(stream: Stream<T>, transformer: (T) -> Stream<R>)
: Stream<R> by FlatteningSequence(stream.toSequence(), { transformer(it).toSequence() })
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.flatMap() instead.")
public class FlatteningSequence<T, R>(private val sequence: Sequence<T>,
private val transformer: (T) -> Sequence<R>
) : Sequence<R> {
@@ -242,6 +248,7 @@ deprecated("Use MultiSequence<T> instead")
public class Multistream<T>(stream: Stream<Stream<T>>)
: Stream<T> by FlatteningSequence(stream.toSequence(), { it.toSequence() })
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.flatten() instead.")
public class MultiSequence<T>(private val sequence: Sequence<Sequence<T>>) : Sequence<T> {
override fun iterator(): Iterator<T> = object : Iterator<T> {
val iterator = sequence.iterator()
@@ -286,6 +293,7 @@ public class TakeStream<T>(stream: Stream<T>, count: Int)
* A sequence that returns at most [count] values from the underlying [sequence], and stops returning values
* as soon as that count is reached.
*/
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.take() instead.")
public class TakeSequence<T>(private val sequence: Sequence<T>,
private val count: Int
) : Sequence<T> {
@@ -317,6 +325,7 @@ public class TakeWhileStream<T>(stream: Stream<T>, predicate: (T) -> Boolean) :
* A sequence that returns values from the underlying [sequence] while the [predicate] function returns
* `true`, and stops returning values once the function returns `false` for the next element.
*/
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.takeWhile() instead.")
public class TakeWhileSequence<T>(private val sequence: Sequence<T>,
private val predicate: (T) -> Boolean
) : Sequence<T> {
@@ -366,6 +375,7 @@ public class DropStream<T>(stream: Stream<T>, count: Int)
* A sequence that skips the specified number of values from the underlying [sequence] and returns
* all values after that.
*/
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.drop() instead.")
public class DropSequence<T>(private val sequence: Sequence<T>,
private val count: Int
) : Sequence<T> {
@@ -404,6 +414,7 @@ public class DropWhileStream<T>(stream: Stream<T>, predicate: (T) -> Boolean) :
* A sequence that skips the values from the underlying [sequence] while the given [predicate] returns `true` and returns
* all values after that.
*/
deprecated("This class is an implementation detail and shall be made internal soon. Use sequence.dropWhile() instead.")
public class DropWhileSequence<T>(private val sequence: Sequence<T>,
private val predicate: (T) -> Boolean
) : Sequence<T> {
@@ -450,7 +461,7 @@ public class DropWhileSequence<T>(private val sequence: Sequence<T>,
* A sequence which repeatedly calls the specified [producer] function and returns its return values, until
* `null` is returned from [producer].
*/
deprecated("Implementation detail. Use function sequence(nextFunction: () -> T?) instead.")
deprecated("This class is an implementation detail and shall be made internal soon. Use function sequence(nextFunction: () -> T?) instead.")
public class FunctionSequence<T : Any>(private val producer: () -> T?) : Sequence<T> {
override fun iterator(): Iterator<T> = object : Iterator<T> {
var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue
@@ -351,7 +351,7 @@ fun filtering(): List<GenericFunction> {
returns(Sequences) { "Sequence<T>" }
body(Sequences) {
"""
return FilteringSequence(this, false, { it == null }) as Sequence<T>
return filterNot { it == null } as Sequence<T>
"""
}
}
@@ -21,7 +21,7 @@ fun generators(): List<GenericFunction> {
returns(Sequences) { "Sequence<T>" }
body(Sequences) {
"""
return MultiSequence(sequenceOf(this, sequenceOf(element)))
return sequenceOf(this, sequenceOf(element)).flatten()
"""
}
}
@@ -58,7 +58,7 @@ fun generators(): List<GenericFunction> {
returns("Sequence<T>")
body {
"""
return MultiSequence(sequenceOf(this, collection.sequence()))
return sequenceOf(this, collection.sequence()).flatten()
"""
}
}
@@ -69,7 +69,7 @@ fun generators(): List<GenericFunction> {
returns("Sequence<T>")
body {
"""
return MultiSequence(sequenceOf(this, sequence))
return sequenceOf(this, sequence).flatten()
"""
}
}
@@ -27,12 +27,7 @@ fun guards(): List<GenericFunction> {
}
body(Sequences) {
"""
return FilteringSequence(this) {
if (it == null) {
throw IllegalArgumentException("null element found in $THIS")
}
true
} as Sequence<T>
return map { it ?: throw IllegalArgumentException("null element found in $THIS") }
"""
}
}
@@ -106,7 +106,7 @@ fun specialJVM(): List<GenericFunction> {
returns(Sequences) { "Sequence<R>" }
body(Sequences) {
"""
return FilteringSequence(this, true, { klass.isInstance(it) }) as Sequence<R>
return filter { klass.isInstance(it) } as Sequence<R>
"""
}
}
@@ -146,7 +146,7 @@ fun specialJVM(): List<GenericFunction> {
receiverAsterisk(true)
body(Sequences) {
"""
return FilteringSequence(this, true, { it is R }) as Sequence<R>
return filter { it is R } as Sequence<R>
"""
}
}