Introduce 'pairwise' function, KEEP-11
This commit is contained in:
@@ -1844,6 +1844,25 @@ public inline fun <T> Iterable<T>.minusElement(element: T): List<T> {
|
||||
return minus(element)
|
||||
}
|
||||
|
||||
@SinceKotlin("1.2")
|
||||
public fun <T> Iterable<T>.pairwise(): List<Pair<T, T>> {
|
||||
return pairwise { a, b -> a to b }
|
||||
}
|
||||
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the original collection into pair of lists,
|
||||
* where *first* list contains elements for which [predicate] yielded `true`,
|
||||
|
||||
@@ -1413,6 +1413,25 @@ public inline fun <T> Sequence<T>.minusElement(element: T): Sequence<T> {
|
||||
return minus(element)
|
||||
}
|
||||
|
||||
@SinceKotlin("1.2")
|
||||
public fun <T> Sequence<T>.pairwise(): Sequence<Pair<T, T>> {
|
||||
return pairwise { a, b -> a to b }
|
||||
}
|
||||
|
||||
@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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the original sequence into pair of lists,
|
||||
* where *first* list contains elements for which [predicate] yielded `true`,
|
||||
|
||||
@@ -1110,6 +1110,22 @@ public inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double {
|
||||
return sum
|
||||
}
|
||||
|
||||
@SinceKotlin("1.2")
|
||||
public fun CharSequence.pairwise(): List<Pair<Char, Char>> {
|
||||
return pairwise { a, b -> a to b }
|
||||
}
|
||||
|
||||
@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
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the original char sequence into pair of char sequences,
|
||||
* where *first* char sequence contains characters for which [predicate] yielded `true`,
|
||||
|
||||
Reference in New Issue
Block a user