Implement reduceIndexedOrNull and reduceRightIndexedOrNull #KT-36866
This commit is contained in:
+23
@@ -1491,6 +1491,15 @@ public final class kotlin/collections/ArraysKt {
|
||||
public static final fun reduceIndexed ([Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceIndexed ([SLkotlin/jvm/functions/Function3;)S
|
||||
public static final fun reduceIndexed ([ZLkotlin/jvm/functions/Function3;)Z
|
||||
public static final fun reduceIndexedOrNull ([BLkotlin/jvm/functions/Function3;)Ljava/lang/Byte;
|
||||
public static final fun reduceIndexedOrNull ([CLkotlin/jvm/functions/Function3;)Ljava/lang/Character;
|
||||
public static final fun reduceIndexedOrNull ([DLkotlin/jvm/functions/Function3;)Ljava/lang/Double;
|
||||
public static final fun reduceIndexedOrNull ([FLkotlin/jvm/functions/Function3;)Ljava/lang/Float;
|
||||
public static final fun reduceIndexedOrNull ([ILkotlin/jvm/functions/Function3;)Ljava/lang/Integer;
|
||||
public static final fun reduceIndexedOrNull ([JLkotlin/jvm/functions/Function3;)Ljava/lang/Long;
|
||||
public static final fun reduceIndexedOrNull ([Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceIndexedOrNull ([SLkotlin/jvm/functions/Function3;)Ljava/lang/Short;
|
||||
public static final fun reduceIndexedOrNull ([ZLkotlin/jvm/functions/Function3;)Ljava/lang/Boolean;
|
||||
public static final fun reduceOrNull ([BLkotlin/jvm/functions/Function2;)Ljava/lang/Byte;
|
||||
public static final fun reduceOrNull ([CLkotlin/jvm/functions/Function2;)Ljava/lang/Character;
|
||||
public static final fun reduceOrNull ([DLkotlin/jvm/functions/Function2;)Ljava/lang/Double;
|
||||
@@ -1518,6 +1527,15 @@ public final class kotlin/collections/ArraysKt {
|
||||
public static final fun reduceRightIndexed ([Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceRightIndexed ([SLkotlin/jvm/functions/Function3;)S
|
||||
public static final fun reduceRightIndexed ([ZLkotlin/jvm/functions/Function3;)Z
|
||||
public static final fun reduceRightIndexedOrNull ([BLkotlin/jvm/functions/Function3;)Ljava/lang/Byte;
|
||||
public static final fun reduceRightIndexedOrNull ([CLkotlin/jvm/functions/Function3;)Ljava/lang/Character;
|
||||
public static final fun reduceRightIndexedOrNull ([DLkotlin/jvm/functions/Function3;)Ljava/lang/Double;
|
||||
public static final fun reduceRightIndexedOrNull ([FLkotlin/jvm/functions/Function3;)Ljava/lang/Float;
|
||||
public static final fun reduceRightIndexedOrNull ([ILkotlin/jvm/functions/Function3;)Ljava/lang/Integer;
|
||||
public static final fun reduceRightIndexedOrNull ([JLkotlin/jvm/functions/Function3;)Ljava/lang/Long;
|
||||
public static final fun reduceRightIndexedOrNull ([Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceRightIndexedOrNull ([SLkotlin/jvm/functions/Function3;)Ljava/lang/Short;
|
||||
public static final fun reduceRightIndexedOrNull ([ZLkotlin/jvm/functions/Function3;)Ljava/lang/Boolean;
|
||||
public static final fun reduceRightOrNull ([BLkotlin/jvm/functions/Function2;)Ljava/lang/Byte;
|
||||
public static final fun reduceRightOrNull ([CLkotlin/jvm/functions/Function2;)Ljava/lang/Character;
|
||||
public static final fun reduceRightOrNull ([DLkotlin/jvm/functions/Function2;)Ljava/lang/Double;
|
||||
@@ -2129,9 +2147,11 @@ public final class kotlin/collections/CollectionsKt {
|
||||
public static final fun randomOrNull (Ljava/util/Collection;Lkotlin/random/Random;)Ljava/lang/Object;
|
||||
public static final fun reduce (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
|
||||
public static final fun reduceIndexed (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceIndexedOrNull (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceOrNull (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
|
||||
public static final fun reduceRight (Ljava/util/List;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
|
||||
public static final fun reduceRightIndexed (Ljava/util/List;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceRightIndexedOrNull (Ljava/util/List;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceRightOrNull (Ljava/util/List;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
|
||||
public static final fun removeAll (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Z
|
||||
public static final fun removeAll (Ljava/util/Collection;Ljava/lang/Iterable;)Z
|
||||
@@ -4660,6 +4680,7 @@ public final class kotlin/sequences/SequencesKt {
|
||||
public static final fun plus (Lkotlin/sequences/Sequence;[Ljava/lang/Object;)Lkotlin/sequences/Sequence;
|
||||
public static final fun reduce (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
|
||||
public static final fun reduceIndexed (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceIndexedOrNull (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function3;)Ljava/lang/Object;
|
||||
public static final fun reduceOrNull (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
|
||||
public static final fun requireNoNulls (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
|
||||
public static final fun scan (Lkotlin/sequences/Sequence;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence;
|
||||
@@ -5058,9 +5079,11 @@ public final class kotlin/text/StringsKt {
|
||||
public static final fun randomOrNull (Ljava/lang/CharSequence;Lkotlin/random/Random;)Ljava/lang/Character;
|
||||
public static final fun reduce (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)C
|
||||
public static final fun reduceIndexed (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function3;)C
|
||||
public static final fun reduceIndexedOrNull (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function3;)Ljava/lang/Character;
|
||||
public static final fun reduceOrNull (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)Ljava/lang/Character;
|
||||
public static final fun reduceRight (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)C
|
||||
public static final fun reduceRightIndexed (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function3;)C
|
||||
public static final fun reduceRightIndexedOrNull (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function3;)Ljava/lang/Character;
|
||||
public static final fun reduceRightOrNull (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function2;)Ljava/lang/Character;
|
||||
public static final fun regionMatches (Ljava/lang/CharSequence;ILjava/lang/CharSequence;IIZ)Z
|
||||
public static final fun regionMatches (Ljava/lang/String;ILjava/lang/String;IIZ)Z
|
||||
|
||||
@@ -708,6 +708,85 @@ object Aggregates : TemplateGroupBase() {
|
||||
}
|
||||
}
|
||||
|
||||
val f_reduceIndexedOrNull = fn("reduceIndexedOrNull(operation: (index: Int, acc: T, T) -> T)") {
|
||||
include(ArraysOfPrimitives, ArraysOfUnsigned, CharSequences)
|
||||
} builder {
|
||||
since("1.4")
|
||||
inline()
|
||||
specialFor(ArraysOfUnsigned) { inlineOnly() }
|
||||
|
||||
doc {
|
||||
"""
|
||||
Accumulates value starting with the first ${f.element} and applying [operation] from left to right
|
||||
to current accumulator value and each ${f.element} with its index in the original ${f.collection}.
|
||||
Returns null if the ${f.collection} is empty.
|
||||
@param [operation] function that takes the index of ${f.element.prefixWithArticle()}, current accumulator value
|
||||
and the ${f.element} itself and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.reduceOrNull")
|
||||
returns("T?")
|
||||
body {
|
||||
"""
|
||||
if (isEmpty())
|
||||
return null
|
||||
|
||||
var accumulator = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_reduceIndexedOrNullSuper = fn("reduceIndexedOrNull(operation: (index: Int, acc: S, T) -> S)") {
|
||||
include(ArraysOfObjects, Iterables, Sequences)
|
||||
} builder {
|
||||
since("1.4")
|
||||
inline()
|
||||
|
||||
doc {
|
||||
"""
|
||||
Accumulates value starting with the first ${f.element} and applying [operation] from left to right
|
||||
to current accumulator value and each ${f.element} with its index in the original ${f.collection}.
|
||||
Returns null if the ${f.collection} is empty.
|
||||
@param [operation] function that takes the index of ${f.element.prefixWithArticle()}, current accumulator value
|
||||
and the ${f.element} itself and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
typeParam("S")
|
||||
typeParam("T : S")
|
||||
sample("samples.collections.Collections.Aggregates.reduceOrNull")
|
||||
returns("S?")
|
||||
body {
|
||||
fun checkOverflow(value: String) = if (f == Sequences || f == Iterables) "checkIndexOverflow($value)" else value
|
||||
"""
|
||||
val iterator = this.iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var index = 1
|
||||
var accumulator: S = iterator.next()
|
||||
while (iterator.hasNext()) {
|
||||
accumulator = operation(${checkOverflow("index++")}, accumulator, iterator.next())
|
||||
}
|
||||
return accumulator
|
||||
"""
|
||||
}
|
||||
body(ArraysOfObjects) {
|
||||
"""
|
||||
if (isEmpty())
|
||||
return null
|
||||
|
||||
var accumulator: S = this[0]
|
||||
for (index in 1..lastIndex) {
|
||||
accumulator = operation(index, accumulator, this[index])
|
||||
}
|
||||
return accumulator
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_reduceRightIndexed = fn("reduceRightIndexed(operation: (index: Int, T, acc: T) -> T)") {
|
||||
include(CharSequences, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
@@ -788,6 +867,90 @@ object Aggregates : TemplateGroupBase() {
|
||||
}
|
||||
}
|
||||
|
||||
val f_reduceRightIndexedOrNull = fn("reduceRightIndexedOrNull(operation: (index: Int, T, acc: T) -> T)") {
|
||||
include(CharSequences, ArraysOfPrimitives, ArraysOfUnsigned)
|
||||
} builder {
|
||||
since("1.4")
|
||||
inline()
|
||||
specialFor(ArraysOfUnsigned) { inlineOnly() }
|
||||
|
||||
doc {
|
||||
"""
|
||||
Accumulates value starting with last ${f.element} and applying [operation] from right to left
|
||||
to each ${f.element} with its index in the original ${f.collection} and current accumulator value.
|
||||
Returns null if the ${f.collection} is empty.
|
||||
@param [operation] function that takes the index of ${f.element.prefixWithArticle()}, the ${f.element} itself
|
||||
and current accumulator value, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.reduceRightOrNull")
|
||||
returns("T?")
|
||||
body {
|
||||
"""
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
|
||||
return accumulator
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_reduceRightIndexedOrNullSuper = fn("reduceRightIndexedOrNull(operation: (index: Int, T, acc: S) -> S)") {
|
||||
include(Lists, ArraysOfObjects)
|
||||
} builder {
|
||||
since("1.4")
|
||||
inline()
|
||||
|
||||
doc {
|
||||
"""
|
||||
Accumulates value starting with last ${f.element} and applying [operation] from right to left
|
||||
to each ${f.element} with its index in the original ${f.collection} and current accumulator value.
|
||||
Returns null if the ${f.collection} is empty.
|
||||
@param [operation] function that takes the index of ${f.element.prefixWithArticle()}, the ${f.element} itself
|
||||
and current accumulator value, and calculates the next accumulator value.
|
||||
"""
|
||||
}
|
||||
sample("samples.collections.Collections.Aggregates.reduceRightOrNull")
|
||||
typeParam("S")
|
||||
typeParam("T : S")
|
||||
returns("S?")
|
||||
body {
|
||||
"""
|
||||
var index = lastIndex
|
||||
if (index < 0) return null
|
||||
|
||||
var accumulator: S = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
|
||||
return accumulator
|
||||
"""
|
||||
}
|
||||
body(Lists) {
|
||||
"""
|
||||
val iterator = listIterator(size)
|
||||
if (!iterator.hasPrevious())
|
||||
return null
|
||||
|
||||
var accumulator: S = iterator.previous()
|
||||
while (iterator.hasPrevious()) {
|
||||
val index = iterator.previousIndex()
|
||||
accumulator = operation(index, iterator.previous(), accumulator)
|
||||
}
|
||||
|
||||
return accumulator
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
val f_reduce = fn("reduce(operation: (acc: T, T) -> T)") {
|
||||
include(ArraysOfPrimitives, ArraysOfUnsigned, CharSequences)
|
||||
} builder {
|
||||
|
||||
Reference in New Issue
Block a user