Rename pairwise to zipWithNext, KEEP-11

This commit is contained in:
Ilya Gorbunov
2017-07-26 23:39:33 +03:00
parent c2cee2e405
commit c04b0072af
16 changed files with 374 additions and 239 deletions
@@ -953,24 +953,11 @@ public expect operator fun <T> Iterable<T>.minus(elements: Sequence<T>): List<T>
@kotlin.internal.InlineOnly
public expect inline fun <T> Iterable<T>.minusElement(element: T): List<T>
/**
* Returns a list of pairs of each two adjacent elements in this collection.
*
* The returned list is empty if this collection contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.pairwise
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()"))
@SinceKotlin("1.2")
public expect fun <T> Iterable<T>.pairwise(): List<Pair<T, T>>
/**
* Returns a list containing the results of applying the given [transform] function
* to an each pair of two adjacent elements in this collection.
*
* The returned list is empty if this collection contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)"))
@SinceKotlin("1.2")
public expect inline fun <T, R> Iterable<T>.pairwise(transform: (a: T, b: T) -> R): List<R>
@@ -1087,6 +1074,27 @@ public expect infix fun <T, R> Iterable<T>.zip(other: Iterable<R>): List<Pair<T,
*/
public expect inline fun <T, R, V> Iterable<T>.zip(other: Iterable<R>, transform: (a: T, b: R) -> V): List<V>
/**
* Returns a list of pairs of each two adjacent elements in this collection.
*
* The returned list is empty if this collection contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.zipWithNext
*/
@SinceKotlin("1.2")
public expect fun <T> Iterable<T>.zipWithNext(): List<Pair<T, T>>
/**
* Returns a list containing the results of applying the given [transform] function
* to an each pair of two adjacent elements in this collection.
*
* The returned list is empty if this collection contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas
*/
@SinceKotlin("1.2")
public expect inline fun <T, R> Iterable<T>.zipWithNext(transform: (a: T, b: T) -> R): List<R>
/**
* Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
*
@@ -881,28 +881,11 @@ public expect operator fun <T> Sequence<T>.minus(elements: Sequence<T>): Sequenc
@kotlin.internal.InlineOnly
public expect inline fun <T> Sequence<T>.minusElement(element: T): Sequence<T>
/**
* Returns a sequence of pairs of each two adjacent elements in this sequence.
*
* The returned sequence is empty if this sequence contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.pairwise
*
* The operation is _intermediate_ and _stateless_.
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()"))
@SinceKotlin("1.2")
public expect fun <T> Sequence<T>.pairwise(): Sequence<Pair<T, T>>
/**
* Returns a sequence containing the results of applying the given [transform] function
* to an each pair of two adjacent elements in this sequence.
*
* The returned sequence is empty if this sequence contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas
*
* The operation is _intermediate_ and _stateless_.
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)"))
@SinceKotlin("1.2")
public expect fun <T, R> Sequence<T>.pairwise(transform: (a: T, b: T) -> R): Sequence<R>
@@ -1009,6 +992,31 @@ public expect infix fun <T, R> Sequence<T>.zip(other: Sequence<R>): Sequence<Pai
*/
public expect fun <T, R, V> Sequence<T>.zip(other: Sequence<R>, transform: (a: T, b: R) -> V): Sequence<V>
/**
* Returns a sequence of pairs of each two adjacent elements in this sequence.
*
* The returned sequence is empty if this sequence contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.zipWithNext
*
* The operation is _intermediate_ and _stateless_.
*/
@SinceKotlin("1.2")
public expect fun <T> Sequence<T>.zipWithNext(): Sequence<Pair<T, T>>
/**
* Returns a sequence containing the results of applying the given [transform] function
* to an each pair of two adjacent elements in this sequence.
*
* The returned sequence is empty if this sequence contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas
*
* The operation is _intermediate_ and _stateless_.
*/
@SinceKotlin("1.2")
public expect fun <T, R> Sequence<T>.zipWithNext(transform: (a: T, b: T) -> R): Sequence<R>
/**
* Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
*
@@ -696,24 +696,11 @@ public expect fun CharSequence.chunkedSequence(size: Int): Sequence<String>
@SinceKotlin("1.2")
public expect fun <R> CharSequence.chunkedSequence(size: Int, transform: (CharSequence) -> R): Sequence<R>
/**
* Returns a list of pairs of each two adjacent characters in this char sequence.
*
* The returned list is empty if this char sequence contains less than two characters.
*
* @sample samples.collections.Collections.Transformations.pairwise
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()"))
@SinceKotlin("1.2")
public expect fun CharSequence.pairwise(): List<Pair<Char, Char>>
/**
* Returns a list containing the results of applying the given [transform] function
* to an each pair of two adjacent characters in this char sequence.
*
* The returned list is empty if this char sequence contains less than two characters.
*
* @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)"))
@SinceKotlin("1.2")
public expect inline fun <R> CharSequence.pairwise(transform: (a: Char, b: Char) -> R): List<R>
@@ -809,6 +796,27 @@ public expect infix fun CharSequence.zip(other: CharSequence): List<Pair<Char, C
*/
public expect inline fun <V> CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List<V>
/**
* Returns a list of pairs of each two adjacent characters in this char sequence.
*
* The returned list is empty if this char sequence contains less than two characters.
*
* @sample samples.collections.Collections.Transformations.zipWithNext
*/
@SinceKotlin("1.2")
public expect fun CharSequence.zipWithNext(): List<Pair<Char, Char>>
/**
* Returns a list containing the results of applying the given [transform] function
* to an each pair of two adjacent characters in this char sequence.
*
* The returned list is empty if this char sequence contains less than two characters.
*
* @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas
*/
@SinceKotlin("1.2")
public expect inline fun <R> CharSequence.zipWithNext(transform: (a: Char, b: Char) -> R): List<R>
/**
* Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated.
*/
@@ -81,18 +81,18 @@ class Collections {
@Sample
fun pairwise() {
fun zipWithNext() {
val letters = ('a'..'f').toList()
val pairs = letters.pairwise()
val pairs = letters.zipWithNext()
assertPrints(letters, "[a, b, c, d, e, f]")
assertPrints(pairs, "[(a, b), (b, c), (c, d), (d, e), (e, f)]")
}
@Sample
fun pairwiseToFindDeltas() {
fun zipWithNextToFindDeltas() {
val values = listOf(1, 4, 9, 16, 25, 36)
val deltas = values.pairwise { a, b -> b - a }
val deltas = values.zipWithNext { a, b -> b - a }
assertPrints(deltas, "[3, 5, 7, 9, 11]")
}
+38 -26
View File
@@ -1877,38 +1877,16 @@ public inline fun <T> Iterable<T>.minusElement(element: T): List<T> {
return minus(element)
}
/**
* Returns a list of pairs of each two adjacent elements in this collection.
*
* The returned list is empty if this collection contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.pairwise
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()"))
@SinceKotlin("1.2")
public fun <T> Iterable<T>.pairwise(): List<Pair<T, T>> {
return pairwise { a, b -> a to b }
return zipWithNext { a, b -> a to b }
}
/**
* Returns a list containing the results of applying the given [transform] function
* to an each pair of two adjacent elements in this collection.
*
* The returned list is empty if this collection contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)"))
@SinceKotlin("1.2")
public inline fun <T, R> Iterable<T>.pairwise(transform: (a: T, b: T) -> R): List<R> {
val iterator = iterator()
if (!iterator.hasNext()) return emptyList()
val result = mutableListOf<R>()
var current = iterator.next()
while (iterator.hasNext()) {
val next = iterator.next()
result.add(transform(current, next))
current = next
}
return result
return zipWithNext(transform)
}
/**
@@ -2145,6 +2123,40 @@ public inline fun <T, R, V> Iterable<T>.zip(other: Iterable<R>, transform: (a: T
return list
}
/**
* Returns a list of pairs of each two adjacent elements in this collection.
*
* The returned list is empty if this collection contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.zipWithNext
*/
@SinceKotlin("1.2")
public fun <T> Iterable<T>.zipWithNext(): List<Pair<T, T>> {
return zipWithNext { a, b -> a to b }
}
/**
* Returns a list containing the results of applying the given [transform] function
* to an each pair of two adjacent elements in this collection.
*
* The returned list is empty if this collection contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas
*/
@SinceKotlin("1.2")
public inline fun <T, R> Iterable<T>.zipWithNext(transform: (a: T, b: T) -> R): List<R> {
val iterator = iterator()
if (!iterator.hasNext()) return emptyList()
val result = mutableListOf<R>()
var current = iterator.next()
while (iterator.hasNext()) {
val next = iterator.next()
result.add(transform(current, next))
current = next
}
return result
}
/**
* Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
*
+42 -30
View File
@@ -1450,42 +1450,16 @@ public inline fun <T> Sequence<T>.minusElement(element: T): Sequence<T> {
return minus(element)
}
/**
* Returns a sequence of pairs of each two adjacent elements in this sequence.
*
* The returned sequence is empty if this sequence contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.pairwise
*
* The operation is _intermediate_ and _stateless_.
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()"))
@SinceKotlin("1.2")
public fun <T> Sequence<T>.pairwise(): Sequence<Pair<T, T>> {
return pairwise { a, b -> a to b }
return zipWithNext { a, b -> a to b }
}
/**
* Returns a sequence containing the results of applying the given [transform] function
* to an each pair of two adjacent elements in this sequence.
*
* The returned sequence is empty if this sequence contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas
*
* The operation is _intermediate_ and _stateless_.
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)"))
@SinceKotlin("1.2")
public fun <T, R> Sequence<T>.pairwise(transform: (a: T, b: T) -> R): Sequence<R> {
return buildSequence result@ {
val iterator = iterator()
if (!iterator.hasNext()) return@result
var current = iterator.next()
while (iterator.hasNext()) {
val next = iterator.next()
yield(transform(current, next))
current = next
}
}
return zipWithNext(transform)
}
/**
@@ -1620,6 +1594,44 @@ public fun <T, R, V> Sequence<T>.zip(other: Sequence<R>, transform: (a: T, b: R)
return MergingSequence(this, other, transform)
}
/**
* Returns a sequence of pairs of each two adjacent elements in this sequence.
*
* The returned sequence is empty if this sequence contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.zipWithNext
*
* The operation is _intermediate_ and _stateless_.
*/
@SinceKotlin("1.2")
public fun <T> Sequence<T>.zipWithNext(): Sequence<Pair<T, T>> {
return zipWithNext { a, b -> a to b }
}
/**
* Returns a sequence containing the results of applying the given [transform] function
* to an each pair of two adjacent elements in this sequence.
*
* The returned sequence is empty if this sequence contains less than two elements.
*
* @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas
*
* The operation is _intermediate_ and _stateless_.
*/
@SinceKotlin("1.2")
public fun <T, R> Sequence<T>.zipWithNext(transform: (a: T, b: T) -> R): Sequence<R> {
return buildSequence result@ {
val iterator = iterator()
if (!iterator.hasNext()) return@result
var current = iterator.next()
while (iterator.hasNext()) {
val next = iterator.next()
yield(transform(current, next))
current = next
}
}
}
/**
* Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
*
+35 -23
View File
@@ -1176,35 +1176,16 @@ public fun <R> CharSequence.chunkedSequence(size: Int, transform: (CharSequence)
return windowedSequence(size, size, transform)
}
/**
* Returns a list of pairs of each two adjacent characters in this char sequence.
*
* The returned list is empty if this char sequence contains less than two characters.
*
* @sample samples.collections.Collections.Transformations.pairwise
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()"))
@SinceKotlin("1.2")
public fun CharSequence.pairwise(): List<Pair<Char, Char>> {
return pairwise { a, b -> a to b }
return zipWithNext { a, b -> a to b }
}
/**
* Returns a list containing the results of applying the given [transform] function
* to an each pair of two adjacent characters in this char sequence.
*
* The returned list is empty if this char sequence contains less than two characters.
*
* @sample samples.collections.Collections.Transformations.pairwiseToFindDeltas
*/
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext(transform)"))
@SinceKotlin("1.2")
public inline fun <R> CharSequence.pairwise(transform: (a: Char, b: Char) -> R): List<R> {
val size = length - 1
if (size < 1) return emptyList()
val result = ArrayList<R>(size)
for (index in 0..size - 1) {
result.add(transform(this[index], this[index + 1]))
}
return result
return zipWithNext(transform)
}
/**
@@ -1347,6 +1328,37 @@ public inline fun <V> CharSequence.zip(other: CharSequence, transform: (a: Char,
return list
}
/**
* Returns a list of pairs of each two adjacent characters in this char sequence.
*
* The returned list is empty if this char sequence contains less than two characters.
*
* @sample samples.collections.Collections.Transformations.zipWithNext
*/
@SinceKotlin("1.2")
public fun CharSequence.zipWithNext(): List<Pair<Char, Char>> {
return zipWithNext { a, b -> a to b }
}
/**
* Returns a list containing the results of applying the given [transform] function
* to an each pair of two adjacent characters in this char sequence.
*
* The returned list is empty if this char sequence contains less than two characters.
*
* @sample samples.collections.Collections.Transformations.zipWithNextToFindDeltas
*/
@SinceKotlin("1.2")
public inline fun <R> CharSequence.zipWithNext(transform: (a: Char, b: Char) -> R): List<R> {
val size = length - 1
if (size < 1) return emptyList()
val result = ArrayList<R>(size)
for (index in 0..size - 1) {
result.add(transform(this[index], this[index + 1]))
}
return result
}
/**
* Creates an [Iterable] instance that wraps the original char sequence returning its characters when being iterated.
*/
@@ -119,21 +119,21 @@ abstract class OrderedIterableTests<T : Iterable<String>>(createFrom: (Array<out
@Test
fun pairwise() {
fun zipWithNext() {
val data = createFrom("", "a", "xyz")
val lengthDeltas = data.pairwise { a, b -> b.length - a.length }
val lengthDeltas = data.zipWithNext { a: String, b: String -> b.length - a.length }
assertEquals(listOf(1, 2), lengthDeltas)
assertTrue(empty.pairwise { a, b -> a + b }.isEmpty())
assertTrue(createFrom("foo").pairwise { a, b -> a + b }.isEmpty())
assertTrue(empty.zipWithNext { a: String, b: String -> a + b }.isEmpty())
assertTrue(createFrom("foo").zipWithNext { a: String, b: String -> a + b }.isEmpty())
}
@Test
fun pairwisePairs() {
assertTrue(empty.pairwise().isEmpty())
assertTrue(createFrom("foo").pairwise().isEmpty())
assertEquals(listOf("a" to "b"), createFrom("a", "b").pairwise())
assertEquals(listOf("a" to "b", "b" to "c"), createFrom("a", "b", "c").pairwise())
fun zipWithNextPairs() {
assertTrue(empty.zipWithNext().isEmpty())
assertTrue(createFrom("foo").zipWithNext().isEmpty())
assertEquals(listOf("a" to "b"), createFrom("a", "b").zipWithNext())
assertEquals(listOf("a" to "b", "b" to "c"), createFrom("a", "b", "c").zipWithNext())
}
@Test
@@ -197,23 +197,23 @@ public class SequenceTest {
assertEquals("", sequenceOf(1).dropWhile { it < 200 }.joinToString(limit = 10))
}
@Test fun pairwise() {
val deltas = fibonacci().pairwise { a, b -> b - a }
@Test fun zipWithNext() {
val deltas = fibonacci().zipWithNext { a: Int, b: Int -> b - a }
// deltas of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...
// is the same sequence prepended by 1
assertEquals(listOf(1) + fibonacci().take(9), deltas.take(10).toList())
ensureIsIntermediate(source = sequenceOf(1, 2)) { it.pairwise { a, b -> b - a } }
ensureIsIntermediate(source = sequenceOf(1, 2)) { it.zipWithNext { a: Int, b: Int -> b - a } }
}
@Test fun pairwisePairs() {
val pairs: Sequence<Pair<String, String>> = sequenceOf("a", "b", "c", "d").pairwise()
@Test fun zipWithNextPairs() {
val pairs: Sequence<Pair<String, String>> = sequenceOf("a", "b", "c", "d").zipWithNext()
assertEquals(listOf("a" to "b", "b" to "c", "c" to "d"), pairs.toList())
assertTrue(emptySequence<String>().pairwise().toList().isEmpty())
assertTrue(sequenceOf(1).pairwise().toList().isEmpty())
assertTrue(emptySequence<String>().zipWithNext().toList().isEmpty())
assertTrue(sequenceOf(1).zipWithNext().toList().isEmpty())
ensureIsIntermediate(source = sequenceOf(1, 2)) { it.pairwise() }
ensureIsIntermediate(source = sequenceOf(1, 2)) { it.zipWithNext() }
}
@Test
+8 -8
View File
@@ -839,16 +839,16 @@ class StringTest {
assertContentEquals("abc", pair.second, "pair.second")
}
@Test fun pairwise() = withOneCharSequenceArg { arg1 ->
assertEquals(listOf("ab", "bc"), arg1("abc").pairwise { a, b -> a.toString() + b })
assertTrue(arg1("").pairwise { a, b -> a.toString() + b }.isEmpty())
assertTrue(arg1("a").pairwise { a, b -> a.toString() + b }.isEmpty())
@Test fun zipWithNext() = withOneCharSequenceArg { arg1 ->
assertEquals(listOf("ab", "bc"), arg1("abc").zipWithNext { a: Char, b: Char -> a.toString() + b })
assertTrue(arg1("").zipWithNext { a: Char, b: Char -> a.toString() + b }.isEmpty())
assertTrue(arg1("a").zipWithNext { a: Char, b: Char -> a.toString() + b }.isEmpty())
}
@Test fun pairwisePairs() = withOneCharSequenceArg { arg1 ->
assertEquals(listOf('a' to 'b', 'b' to 'c'), arg1("abc").pairwise())
assertTrue(arg1("").pairwise().isEmpty())
assertTrue(arg1("a").pairwise().isEmpty())
@Test fun zipWithNextPairs() = withOneCharSequenceArg { arg1 ->
assertEquals(listOf('a' to 'b', 'b' to 'c'), arg1("abc").zipWithNext())
assertTrue(arg1("").zipWithNext().isEmpty())
assertTrue(arg1("a").zipWithNext().isEmpty())
}