From 58310d172f54c19d42cc0a8df48cbfb0fe626c37 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 15 Apr 2015 20:05:47 +0300 Subject: [PATCH] Deprecate sequence implementations in favor of sequence operations. --- libraries/stdlib/src/generated/_Filtering.kt | 4 ++-- libraries/stdlib/src/generated/_Generators.kt | 12 ++++++------ libraries/stdlib/src/generated/_Guards.kt | 14 ++------------ libraries/stdlib/src/generated/_SpecialJVM.kt | 8 ++++---- .../stdlib/src/kotlin/collections/Operations.kt | 2 +- .../stdlib/src/kotlin/collections/Sequence.kt | 13 ++++++++++++- .../kotlin-stdlib-gen/src/templates/Filtering.kt | 2 +- .../kotlin-stdlib-gen/src/templates/Generators.kt | 6 +++--- .../kotlin-stdlib-gen/src/templates/Guards.kt | 7 +------ .../kotlin-stdlib-gen/src/templates/SpecialJVM.kt | 4 ++-- 10 files changed, 34 insertions(+), 38 deletions(-) diff --git a/libraries/stdlib/src/generated/_Filtering.kt b/libraries/stdlib/src/generated/_Filtering.kt index 3666e5e9d8d..e5d1188228b 100644 --- a/libraries/stdlib/src/generated/_Filtering.kt +++ b/libraries/stdlib/src/generated/_Filtering.kt @@ -579,7 +579,7 @@ public fun Iterable.filterNotNull(): List { * Returns a sequence containing all elements that are not null */ public fun Sequence.filterNotNull(): Sequence { - return FilteringSequence(this, false, { it == null }) as Sequence + return filterNot { it == null } as Sequence } @@ -588,7 +588,7 @@ deprecated("Migrate to using Sequence and respective functions") * Returns a stream containing all elements that are not null */ public fun Stream.filterNotNull(): Stream { - return FilteringStream(this, false, { it == null }) as Stream + return filterNot { it == null } as Stream } /** diff --git a/libraries/stdlib/src/generated/_Generators.kt b/libraries/stdlib/src/generated/_Generators.kt index 488eb5765ab..96e64e1f3b8 100644 --- a/libraries/stdlib/src/generated/_Generators.kt +++ b/libraries/stdlib/src/generated/_Generators.kt @@ -705,7 +705,7 @@ public fun Iterable.plus(collection: Iterable): List { * Returns a sequence containing all elements of original sequence and then all elements of the given [collection] */ public fun Sequence.plus(collection: Iterable): Sequence { - return MultiSequence(sequenceOf(this, collection.sequence())) + return sequenceOf(this, collection.sequence()).flatten() } @@ -714,7 +714,7 @@ deprecated("Migrate to using Sequence and respective functions") * Returns a stream containing all elements of original stream and then all elements of the given [collection] */ public fun Stream.plus(collection: Iterable): Stream { - return Multistream(streamOf(this, collection.stream())) + return streamOf(this, collection.stream()).flatten() } /** @@ -811,7 +811,7 @@ public fun Iterable.plus(element: T): List { * Returns a sequence containing all elements of original sequence and then the given element */ public fun Sequence.plus(element: T): Sequence { - return MultiSequence(sequenceOf(this, sequenceOf(element))) + return sequenceOf(this, sequenceOf(element)).flatten() } @@ -820,14 +820,14 @@ deprecated("Migrate to using Sequence and respective functions") * Returns a stream containing all elements of original stream and then the given element */ public fun Stream.plus(element: T): Stream { - 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 Sequence.plus(sequence: Sequence): Sequence { - return MultiSequence(sequenceOf(this, sequence)) + return sequenceOf(this, sequence).flatten() } @@ -836,7 +836,7 @@ deprecated("Migrate to using Sequence and respective functions") * Returns a stream containing all elements of original stream and then all elements of the given [stream] */ public fun Stream.plus(stream: Stream): Stream { - return Multistream(streamOf(this, stream)) + return streamOf(this, stream).flatten() } /** diff --git a/libraries/stdlib/src/generated/_Guards.kt b/libraries/stdlib/src/generated/_Guards.kt index 27bf0fd24e6..f1458441efb 100644 --- a/libraries/stdlib/src/generated/_Guards.kt +++ b/libraries/stdlib/src/generated/_Guards.kt @@ -49,12 +49,7 @@ public fun List.requireNoNulls(): List { * Returns an original collection containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements */ public fun Sequence.requireNoNulls(): Sequence { - return FilteringSequence(this) { - if (it == null) { - throw IllegalArgumentException("null element found in $this") - } - true - } as Sequence + return map { it ?: throw IllegalArgumentException("null element found in $this") } } @@ -63,11 +58,6 @@ deprecated("Migrate to using Sequence and respective functions") * Returns an original collection containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements */ public fun Stream.requireNoNulls(): Stream { - return FilteringStream(this) { - if (it == null) { - throw IllegalArgumentException("null element found in $this") - } - true - } as Stream + return map { it ?: throw IllegalArgumentException("null element found in $this") } } diff --git a/libraries/stdlib/src/generated/_SpecialJVM.kt b/libraries/stdlib/src/generated/_SpecialJVM.kt index 9582f1047b1..670f6f836a3 100644 --- a/libraries/stdlib/src/generated/_SpecialJVM.kt +++ b/libraries/stdlib/src/generated/_SpecialJVM.kt @@ -471,7 +471,7 @@ public inline fun Iterable<*>.filterIsInstance(): List { * Returns a sequence containing all elements that are instances of specified type parameter R */ public inline fun Sequence<*>.filterIsInstance(): Sequence { - return FilteringSequence(this, true, { it is R }) as Sequence + return filter { it is R } as Sequence } @@ -480,7 +480,7 @@ deprecated("Migrate to using Sequence and respective functions") * Returns a stream containing all elements that are instances of specified type parameter R */ public inline fun Stream<*>.filterIsInstance(): Stream { - return FilteringStream(this, true, { it is R }) as Stream + return filter { it is R } as Stream } /** @@ -501,7 +501,7 @@ public fun Iterable<*>.filterIsInstance(klass: Class): List { * Returns a sequence containing all elements that are instances of specified class */ public fun Sequence<*>.filterIsInstance(klass: Class): Sequence { - return FilteringSequence(this, true, { klass.isInstance(it) }) as Sequence + return filter { klass.isInstance(it) } as Sequence } @@ -510,7 +510,7 @@ deprecated("Migrate to using Sequence and respective functions") * Returns a stream containing all elements that are instances of specified class */ public fun Stream<*>.filterIsInstance(klass: Class): Stream { - return FilteringStream(this, true, { klass.isInstance(it) }) as Stream + return filter { klass.isInstance(it) } as Stream } /** diff --git a/libraries/stdlib/src/kotlin/collections/Operations.kt b/libraries/stdlib/src/kotlin/collections/Operations.kt index 58c232345d2..a361a1412f5 100644 --- a/libraries/stdlib/src/kotlin/collections/Operations.kt +++ b/libraries/stdlib/src/kotlin/collections/Operations.kt @@ -18,7 +18,7 @@ public fun Iterable>.flatten(): List { * Returns a sequence of all elements from all sequences in this sequence. */ public fun Sequence>.flatten(): Sequence { - return FlatteningSequence(this, { it }) + return MultiSequence(this) } /** diff --git a/libraries/stdlib/src/kotlin/collections/Sequence.kt b/libraries/stdlib/src/kotlin/collections/Sequence.kt index 155358f9b8f..15d80be6289 100644 --- a/libraries/stdlib/src/kotlin/collections/Sequence.kt +++ b/libraries/stdlib/src/kotlin/collections/Sequence.kt @@ -63,6 +63,7 @@ public class FilteringStream(stream: Stream, 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(private val sequence: Sequence, private val sendWhen: Boolean = true, private val predicate: (T) -> Boolean @@ -112,6 +113,7 @@ public class TransformingStream(stream: Stream, 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(private val sequence: Sequence, private val transformer: (T) -> R) : Sequence { override fun iterator(): Iterator = object : Iterator { val iterator = sequence.iterator() @@ -134,6 +136,7 @@ public class TransformingIndexedStream(stream: Stream, 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(private val sequence: Sequence, private val transformer: (Int, T) -> R) : Sequence { override fun iterator(): Iterator = object : Iterator { val iterator = sequence.iterator() @@ -156,6 +159,7 @@ public class IndexingStream(stream: Stream) * 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(private val sequence: Sequence) : Sequence> { override fun iterator(): Iterator> = object : Iterator> { val iterator = sequence.iterator() @@ -179,6 +183,7 @@ public class MergingStream(stream1: Stream, stream2: Stream, * [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(private val sequence1: Sequence, private val sequence2: Sequence, private val transform: (T1, T2) -> V @@ -200,6 +205,7 @@ deprecated("Use FlatteningSequence instead") public class FlatteningStream(stream: Stream, transformer: (T) -> Stream) : Stream 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(private val sequence: Sequence, private val transformer: (T) -> Sequence ) : Sequence { @@ -242,6 +248,7 @@ deprecated("Use MultiSequence instead") public class Multistream(stream: Stream>) : Stream 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(private val sequence: Sequence>) : Sequence { override fun iterator(): Iterator = object : Iterator { val iterator = sequence.iterator() @@ -286,6 +293,7 @@ public class TakeStream(stream: Stream, 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(private val sequence: Sequence, private val count: Int ) : Sequence { @@ -317,6 +325,7 @@ public class TakeWhileStream(stream: Stream, 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(private val sequence: Sequence, private val predicate: (T) -> Boolean ) : Sequence { @@ -366,6 +375,7 @@ public class DropStream(stream: Stream, 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(private val sequence: Sequence, private val count: Int ) : Sequence { @@ -404,6 +414,7 @@ public class DropWhileStream(stream: Stream, 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(private val sequence: Sequence, private val predicate: (T) -> Boolean ) : Sequence { @@ -450,7 +461,7 @@ public class DropWhileSequence(private val sequence: Sequence, * 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(private val producer: () -> T?) : Sequence { override fun iterator(): Iterator = object : Iterator { var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt index 6845d4428f3..71dc776db15 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt @@ -351,7 +351,7 @@ fun filtering(): List { returns(Sequences) { "Sequence" } body(Sequences) { """ - return FilteringSequence(this, false, { it == null }) as Sequence + return filterNot { it == null } as Sequence """ } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index f28cc333718..c5781c742d7 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -21,7 +21,7 @@ fun generators(): List { returns(Sequences) { "Sequence" } body(Sequences) { """ - return MultiSequence(sequenceOf(this, sequenceOf(element))) + return sequenceOf(this, sequenceOf(element)).flatten() """ } } @@ -58,7 +58,7 @@ fun generators(): List { returns("Sequence") body { """ - return MultiSequence(sequenceOf(this, collection.sequence())) + return sequenceOf(this, collection.sequence()).flatten() """ } } @@ -69,7 +69,7 @@ fun generators(): List { returns("Sequence") body { """ - return MultiSequence(sequenceOf(this, sequence)) + return sequenceOf(this, sequence).flatten() """ } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt index 07d43bb4a49..f924fc323b8 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt @@ -27,12 +27,7 @@ fun guards(): List { } body(Sequences) { """ - return FilteringSequence(this) { - if (it == null) { - throw IllegalArgumentException("null element found in $THIS") - } - true - } as Sequence + return map { it ?: throw IllegalArgumentException("null element found in $THIS") } """ } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index 669f7fa9e21..78ccbef2dc5 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -106,7 +106,7 @@ fun specialJVM(): List { returns(Sequences) { "Sequence" } body(Sequences) { """ - return FilteringSequence(this, true, { klass.isInstance(it) }) as Sequence + return filter { klass.isInstance(it) } as Sequence """ } } @@ -146,7 +146,7 @@ fun specialJVM(): List { receiverAsterisk(true) body(Sequences) { """ - return FilteringSequence(this, true, { it is R }) as Sequence + return filter { it is R } as Sequence """ } }