Rename sequence function to generateSequence

This commit is contained in:
Ilya Gorbunov
2016-01-18 21:44:24 +03:00
parent 63e8865123
commit 7896e58afc
4 changed files with 36 additions and 25 deletions
+1 -1
View File
@@ -80,7 +80,7 @@ public class Regex(pattern: String, options: Set<RegexOption>) {
/** 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<MatchResult> = sequence({ find(input, startIndex) }, { match -> match.next() })
public fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> = generateSequence({ find(input, startIndex) }, { match -> match.next() })
@Deprecated("Use findAll() instead.", ReplaceWith("findAll(input, startIndex)"))
public fun matchAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> = findAll(input, startIndex)
@@ -492,27 +492,38 @@ private class ConstrainedOnceSequence<T>(sequence: Sequence<T>) : Sequence<T> {
*
* @see constrainOnce
*/
public fun <T : Any> sequence(nextFunction: () -> T?): Sequence<T> {
public fun <T : Any> generateSequence(nextFunction: () -> T?): Sequence<T> {
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 <T : Any> sequence(initialValue: T?, nextFunction: (T) -> T?): Sequence<T> =
if (initialValue == null)
EmptySequence
else
GeneratorSequence({ initialValue }, nextFunction)
@Deprecated("Use generateSequence instead.", ReplaceWith("generateSequence(nextFunction)"))
public fun <T : Any> sequence(nextFunction: () -> T?): Sequence<T> = 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 <T: Any> sequence(initialValueFunction: () -> T?, nextFunction: (T) -> T?): Sequence<T> =
GeneratorSequence(initialValueFunction, nextFunction)
@kotlin.internal.LowPriorityInOverloadResolution
public fun <T : Any> generateSequence(seed: T?, nextFunction: (T) -> T?): Sequence<T> =
if (seed == null)
EmptySequence
else
GeneratorSequence({ seed }, nextFunction)
@Deprecated("Use generateSequence instead.", ReplaceWith("generateSequence(initialValue, nextFunction)"))
@kotlin.internal.LowPriorityInOverloadResolution
public fun <T : Any> sequence(initialValue: T?, nextFunction: (T) -> T?): Sequence<T> = 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 <T: Any> generateSequence(seedFunction: () -> T?, nextFunction: (T) -> T?): Sequence<T> =
GeneratorSequence(seedFunction, nextFunction)
@Deprecated("Use generateSequence instead.", ReplaceWith("generateSequence(initialValueFunction, nextFunction)"))
public fun <T: Any> sequence(initialValueFunction: () -> T?, nextFunction: (T) -> T?): Sequence<T> = generateSequence(initialValueFunction, nextFunction)
@@ -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<MatchResult> = sequence({ find(input, startIndex) }, { match -> match.next() })
public fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> = 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<MatchResult> = findAll(input, startIndex)
@@ -9,7 +9,7 @@ import kotlin.comparisons.*
fun fibonacci(): Sequence<Int> {
// 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<Int>({ (throw IllegalStateException()) }, { null })
val errorValues = generateSequence<Int>({ (throw IllegalStateException()) }, { null })
// does not throw on iteration
val iterator = errorValues.iterator()
// throws on advancing