From 7896e58afcb8c76a33e66d38334d2a7ad1698260 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 18 Jan 2016 21:44:24 +0300 Subject: [PATCH] Rename sequence function to generateSequence --- js/js.libraries/src/core/regex.kt | 2 +- .../src/kotlin/collections/Sequences.kt | 47 ++++++++++++------- .../stdlib/src/kotlin/text/regex/RegexJVM.kt | 2 +- .../stdlib/test/collections/SequenceTest.kt | 10 ++-- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/js/js.libraries/src/core/regex.kt b/js/js.libraries/src/core/regex.kt index 9c2b58833b9..729fecd9bab 100644 --- a/js/js.libraries/src/core/regex.kt +++ b/js/js.libraries/src/core/regex.kt @@ -80,7 +80,7 @@ public class Regex(pattern: String, options: Set) { /** Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex]. */ - public fun findAll(input: CharSequence, startIndex: Int = 0): Sequence = sequence({ find(input, startIndex) }, { match -> match.next() }) + public fun findAll(input: CharSequence, startIndex: Int = 0): Sequence = generateSequence({ find(input, startIndex) }, { match -> match.next() }) @Deprecated("Use findAll() instead.", ReplaceWith("findAll(input, startIndex)")) public fun matchAll(input: CharSequence, startIndex: Int = 0): Sequence = findAll(input, startIndex) diff --git a/libraries/stdlib/src/kotlin/collections/Sequences.kt b/libraries/stdlib/src/kotlin/collections/Sequences.kt index 99bdfa31d86..4b9c9fdb560 100644 --- a/libraries/stdlib/src/kotlin/collections/Sequences.kt +++ b/libraries/stdlib/src/kotlin/collections/Sequences.kt @@ -492,27 +492,38 @@ private class ConstrainedOnceSequence(sequence: Sequence) : Sequence { * * @see constrainOnce */ -public fun sequence(nextFunction: () -> T?): Sequence { +public fun generateSequence(nextFunction: () -> T?): Sequence { return GeneratorSequence(nextFunction, { nextFunction() }).constrainOnce() } -/** - * Returns a sequence which invokes the function to calculate the next value based on the previous one on each iteration - * until the function returns `null`. The sequence starts with the specified [initialValue]. - * - * The sequence can be iterated multiple times, each time starting with the [initialValue]. - */ -@kotlin.internal.LowPriorityInOverloadResolution -public fun sequence(initialValue: T?, nextFunction: (T) -> T?): Sequence = - if (initialValue == null) - EmptySequence - else - GeneratorSequence({ initialValue }, nextFunction) +@Deprecated("Use generateSequence instead.", ReplaceWith("generateSequence(nextFunction)")) +public fun sequence(nextFunction: () -> T?): Sequence = generateSequence(nextFunction) /** - * Returns a sequence which invokes the function [initialValueFunction] to get the first item and then - * [nextFunction] to calculate the next value based on the previous one on each iteration - * until the function returns `null`. The sequence starts with the value returned by [initialValueFunction]. + * Returns a sequence which invokes the function to calculate the next value based on the previous one on each iteration + * until the function returns `null`. The sequence starts with the specified [seed]. + * + * The sequence can be iterated multiple times, each time starting with the [seed]. */ -public fun sequence(initialValueFunction: () -> T?, nextFunction: (T) -> T?): Sequence = - GeneratorSequence(initialValueFunction, nextFunction) +@kotlin.internal.LowPriorityInOverloadResolution +public fun generateSequence(seed: T?, nextFunction: (T) -> T?): Sequence = + if (seed == null) + EmptySequence + else + GeneratorSequence({ seed }, nextFunction) + +@Deprecated("Use generateSequence instead.", ReplaceWith("generateSequence(initialValue, nextFunction)")) +@kotlin.internal.LowPriorityInOverloadResolution +public fun sequence(initialValue: T?, nextFunction: (T) -> T?): Sequence = generateSequence(initialValue, nextFunction) + +/** + * Returns a sequence which invokes the function [seedFunction] to get the first item and then + * [nextFunction] to calculate the next value based on the previous one on each iteration + * until the function returns `null`. The sequence starts with the value returned by [seedFunction]. + */ +public fun generateSequence(seedFunction: () -> T?, nextFunction: (T) -> T?): Sequence = + GeneratorSequence(seedFunction, nextFunction) + + +@Deprecated("Use generateSequence instead.", ReplaceWith("generateSequence(initialValueFunction, nextFunction)")) +public fun sequence(initialValueFunction: () -> T?, nextFunction: (T) -> T?): Sequence = generateSequence(initialValueFunction, nextFunction) diff --git a/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt b/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt index d6bbffe7887..bd0504d94db 100644 --- a/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt +++ b/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt @@ -137,7 +137,7 @@ public class Regex internal constructor(private val nativePattern: Pattern) { /** * Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex]. */ - public fun findAll(input: CharSequence, startIndex: Int = 0): Sequence = sequence({ find(input, startIndex) }, { match -> match.next() }) + public fun findAll(input: CharSequence, startIndex: Int = 0): Sequence = generateSequence({ find(input, startIndex) }, { match -> match.next() }) @Deprecated("Use findAll() instead.", ReplaceWith("findAll(input, startIndex)"), DeprecationLevel.ERROR) public fun matchAll(input: CharSequence, startIndex: Int = 0): Sequence = findAll(input, startIndex) diff --git a/libraries/stdlib/test/collections/SequenceTest.kt b/libraries/stdlib/test/collections/SequenceTest.kt index a5f966443ca..4124d4da86c 100644 --- a/libraries/stdlib/test/collections/SequenceTest.kt +++ b/libraries/stdlib/test/collections/SequenceTest.kt @@ -9,7 +9,7 @@ import kotlin.comparisons.* fun fibonacci(): Sequence { // fibonacci terms // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, ... - return sequence(Pair(0, 1), { Pair(it.second, it.first + it.second) }).map { it.first } + return generateSequence(Pair(0, 1), { Pair(it.second, it.first + it.second) }).map { it.first } } public class SequenceTest { @@ -215,7 +215,7 @@ public class SequenceTest { @test fun sequenceFromFunction() { var count = 3 - val sequence = sequence { + val sequence = generateSequence { count-- if (count >= 0) count else null } @@ -229,7 +229,7 @@ public class SequenceTest { } @test fun sequenceFromFunctionWithInitialValue() { - val values = sequence(3) { n -> if (n > 0) n - 1 else null } + val values = generateSequence(3) { n -> if (n > 0) n - 1 else null } val expected = listOf(3, 2, 1, 0) assertEquals(expected, values.toList()) assertEquals(expected, values.toList(), "Iterating sequence second time yields the same result") @@ -237,7 +237,7 @@ public class SequenceTest { @Test fun sequenceFromFunctionWithLazyInitialValue() { var start = 3 - val values = sequence({ start }, { n -> if (n > 0) n - 1 else null }) + val values = generateSequence({ start }, { n -> if (n > 0) n - 1 else null }) val expected = listOf(3, 2, 1, 0) assertEquals(expected, values.toList()) assertEquals(expected, values.toList(), "Iterating sequence second time yields the same result") @@ -246,7 +246,7 @@ public class SequenceTest { assertEquals(expected.drop(1), values.toList(), "Initial value function is called on each iterator request") // does not throw on construction - val errorValues = sequence({ (throw IllegalStateException()) }, { null }) + val errorValues = generateSequence({ (throw IllegalStateException()) }, { null }) // does not throw on iteration val iterator = errorValues.iterator() // throws on advancing