KT-20357: Add sample for reduce, reduceRight and their indexed counterparts

This commit is contained in:
Tillmann Berg
2019-12-07 00:08:24 +01:00
committed by Ilya Gorbunov
parent 6d092b5f71
commit 95300ae31d
7 changed files with 150 additions and 0 deletions
@@ -12472,6 +12472,8 @@ public inline fun CharArray.none(predicate: (Char) -> Boolean): Boolean {
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun <S, T : S> Array<out T>.reduce(operation: (acc: S, T) -> S): S {
if (isEmpty())
@@ -12485,6 +12487,8 @@ public inline fun <S, T : S> Array<out T>.reduce(operation: (acc: S, T) -> S): S
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun ByteArray.reduce(operation: (acc: Byte, Byte) -> Byte): Byte {
if (isEmpty())
@@ -12498,6 +12502,8 @@ public inline fun ByteArray.reduce(operation: (acc: Byte, Byte) -> Byte): Byte {
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun ShortArray.reduce(operation: (acc: Short, Short) -> Short): Short {
if (isEmpty())
@@ -12511,6 +12517,8 @@ public inline fun ShortArray.reduce(operation: (acc: Short, Short) -> Short): Sh
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun IntArray.reduce(operation: (acc: Int, Int) -> Int): Int {
if (isEmpty())
@@ -12524,6 +12532,8 @@ public inline fun IntArray.reduce(operation: (acc: Int, Int) -> Int): Int {
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun LongArray.reduce(operation: (acc: Long, Long) -> Long): Long {
if (isEmpty())
@@ -12537,6 +12547,8 @@ public inline fun LongArray.reduce(operation: (acc: Long, Long) -> Long): Long {
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun FloatArray.reduce(operation: (acc: Float, Float) -> Float): Float {
if (isEmpty())
@@ -12550,6 +12562,8 @@ public inline fun FloatArray.reduce(operation: (acc: Float, Float) -> Float): Fl
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun DoubleArray.reduce(operation: (acc: Double, Double) -> Double): Double {
if (isEmpty())
@@ -12563,6 +12577,8 @@ public inline fun DoubleArray.reduce(operation: (acc: Double, Double) -> Double)
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun BooleanArray.reduce(operation: (acc: Boolean, Boolean) -> Boolean): Boolean {
if (isEmpty())
@@ -12576,6 +12592,8 @@ public inline fun BooleanArray.reduce(operation: (acc: Boolean, Boolean) -> Bool
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun CharArray.reduce(operation: (acc: Char, Char) -> Char): Char {
if (isEmpty())
@@ -12592,6 +12610,8 @@ public inline fun CharArray.reduce(operation: (acc: Char, Char) -> Char): Char {
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun <S, T : S> Array<out T>.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S {
if (isEmpty())
@@ -12608,6 +12628,8 @@ public inline fun <S, T : S> Array<out T>.reduceIndexed(operation: (index: Int,
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun ByteArray.reduceIndexed(operation: (index: Int, acc: Byte, Byte) -> Byte): Byte {
if (isEmpty())
@@ -12624,6 +12646,8 @@ public inline fun ByteArray.reduceIndexed(operation: (index: Int, acc: Byte, Byt
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun ShortArray.reduceIndexed(operation: (index: Int, acc: Short, Short) -> Short): Short {
if (isEmpty())
@@ -12640,6 +12664,8 @@ public inline fun ShortArray.reduceIndexed(operation: (index: Int, acc: Short, S
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun IntArray.reduceIndexed(operation: (index: Int, acc: Int, Int) -> Int): Int {
if (isEmpty())
@@ -12656,6 +12682,8 @@ public inline fun IntArray.reduceIndexed(operation: (index: Int, acc: Int, Int)
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun LongArray.reduceIndexed(operation: (index: Int, acc: Long, Long) -> Long): Long {
if (isEmpty())
@@ -12672,6 +12700,8 @@ public inline fun LongArray.reduceIndexed(operation: (index: Int, acc: Long, Lon
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun FloatArray.reduceIndexed(operation: (index: Int, acc: Float, Float) -> Float): Float {
if (isEmpty())
@@ -12688,6 +12718,8 @@ public inline fun FloatArray.reduceIndexed(operation: (index: Int, acc: Float, F
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun DoubleArray.reduceIndexed(operation: (index: Int, acc: Double, Double) -> Double): Double {
if (isEmpty())
@@ -12704,6 +12736,8 @@ public inline fun DoubleArray.reduceIndexed(operation: (index: Int, acc: Double,
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun BooleanArray.reduceIndexed(operation: (index: Int, acc: Boolean, Boolean) -> Boolean): Boolean {
if (isEmpty())
@@ -12720,6 +12754,8 @@ public inline fun BooleanArray.reduceIndexed(operation: (index: Int, acc: Boolea
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun CharArray.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char {
if (isEmpty())
@@ -12886,6 +12922,8 @@ public inline fun CharArray.reduceOrNull(operation: (acc: Char, Char) -> Char):
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun <S, T : S> Array<out T>.reduceRight(operation: (T, acc: S) -> S): S {
var index = lastIndex
@@ -12899,6 +12937,8 @@ public inline fun <S, T : S> Array<out T>.reduceRight(operation: (T, acc: S) ->
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun ByteArray.reduceRight(operation: (Byte, acc: Byte) -> Byte): Byte {
var index = lastIndex
@@ -12912,6 +12952,8 @@ public inline fun ByteArray.reduceRight(operation: (Byte, acc: Byte) -> Byte): B
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun ShortArray.reduceRight(operation: (Short, acc: Short) -> Short): Short {
var index = lastIndex
@@ -12925,6 +12967,8 @@ public inline fun ShortArray.reduceRight(operation: (Short, acc: Short) -> Short
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun IntArray.reduceRight(operation: (Int, acc: Int) -> Int): Int {
var index = lastIndex
@@ -12938,6 +12982,8 @@ public inline fun IntArray.reduceRight(operation: (Int, acc: Int) -> Int): Int {
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun LongArray.reduceRight(operation: (Long, acc: Long) -> Long): Long {
var index = lastIndex
@@ -12951,6 +12997,8 @@ public inline fun LongArray.reduceRight(operation: (Long, acc: Long) -> Long): L
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun FloatArray.reduceRight(operation: (Float, acc: Float) -> Float): Float {
var index = lastIndex
@@ -12964,6 +13012,8 @@ public inline fun FloatArray.reduceRight(operation: (Float, acc: Float) -> Float
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun DoubleArray.reduceRight(operation: (Double, acc: Double) -> Double): Double {
var index = lastIndex
@@ -12977,6 +13027,8 @@ public inline fun DoubleArray.reduceRight(operation: (Double, acc: Double) -> Do
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun BooleanArray.reduceRight(operation: (Boolean, acc: Boolean) -> Boolean): Boolean {
var index = lastIndex
@@ -12990,6 +13042,8 @@ public inline fun BooleanArray.reduceRight(operation: (Boolean, acc: Boolean) ->
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun CharArray.reduceRight(operation: (Char, acc: Char) -> Char): Char {
var index = lastIndex
@@ -13006,6 +13060,8 @@ public inline fun CharArray.reduceRight(operation: (Char, acc: Char) -> Char): C
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun <S, T : S> Array<out T>.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S {
var index = lastIndex
@@ -13023,6 +13079,8 @@ public inline fun <S, T : S> Array<out T>.reduceRightIndexed(operation: (index:
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun ByteArray.reduceRightIndexed(operation: (index: Int, Byte, acc: Byte) -> Byte): Byte {
var index = lastIndex
@@ -13040,6 +13098,8 @@ public inline fun ByteArray.reduceRightIndexed(operation: (index: Int, Byte, acc
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun ShortArray.reduceRightIndexed(operation: (index: Int, Short, acc: Short) -> Short): Short {
var index = lastIndex
@@ -13057,6 +13117,8 @@ public inline fun ShortArray.reduceRightIndexed(operation: (index: Int, Short, a
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun IntArray.reduceRightIndexed(operation: (index: Int, Int, acc: Int) -> Int): Int {
var index = lastIndex
@@ -13074,6 +13136,8 @@ public inline fun IntArray.reduceRightIndexed(operation: (index: Int, Int, acc:
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun LongArray.reduceRightIndexed(operation: (index: Int, Long, acc: Long) -> Long): Long {
var index = lastIndex
@@ -13091,6 +13155,8 @@ public inline fun LongArray.reduceRightIndexed(operation: (index: Int, Long, acc
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun FloatArray.reduceRightIndexed(operation: (index: Int, Float, acc: Float) -> Float): Float {
var index = lastIndex
@@ -13108,6 +13174,8 @@ public inline fun FloatArray.reduceRightIndexed(operation: (index: Int, Float, a
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun DoubleArray.reduceRightIndexed(operation: (index: Int, Double, acc: Double) -> Double): Double {
var index = lastIndex
@@ -13125,6 +13193,8 @@ public inline fun DoubleArray.reduceRightIndexed(operation: (index: Int, Double,
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun BooleanArray.reduceRightIndexed(operation: (index: Int, Boolean, acc: Boolean) -> Boolean): Boolean {
var index = lastIndex
@@ -13142,6 +13212,8 @@ public inline fun BooleanArray.reduceRightIndexed(operation: (index: Int, Boolea
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun CharArray.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char {
var index = lastIndex
@@ -1838,6 +1838,8 @@ public inline fun <T, C : Iterable<T>> C.onEach(action: (T) -> Unit): C {
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun <S, T : S> Iterable<T>.reduce(operation: (acc: S, T) -> S): S {
val iterator = this.iterator()
@@ -1854,6 +1856,8 @@ public inline fun <S, T : S> Iterable<T>.reduce(operation: (acc: S, T) -> S): S
* to current accumulator value and each element with its index in the original collection.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun <S, T : S> Iterable<T>.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S {
val iterator = this.iterator()
@@ -1885,6 +1889,8 @@ public inline fun <S, T : S> Iterable<T>.reduceOrNull(operation: (acc: S, T) ->
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun <S, T : S> List<T>.reduceRight(operation: (T, acc: S) -> S): S {
val iterator = listIterator(size)
@@ -1902,6 +1908,8 @@ public inline fun <S, T : S> List<T>.reduceRight(operation: (T, acc: S) -> S): S
* to each element with its index in the original list and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun <S, T : S> List<T>.reduceRightIndexed(operation: (index: Int, T, acc: S) -> S): S {
val iterator = listIterator(size)
@@ -1352,6 +1352,8 @@ public fun <T> Sequence<T>.onEach(action: (T) -> Unit): Sequence<T> {
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* The operation is _terminal_.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun <S, T : S> Sequence<T>.reduce(operation: (acc: S, T) -> S): S {
val iterator = this.iterator()
@@ -1370,6 +1372,8 @@ public inline fun <S, T : S> Sequence<T>.reduce(operation: (acc: S, T) -> S): S
* and the element itself and calculates the next accumulator value.
*
* The operation is _terminal_.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun <S, T : S> Sequence<T>.reduceIndexed(operation: (index: Int, acc: S, T) -> S): S {
val iterator = this.iterator()
@@ -1168,6 +1168,8 @@ public inline fun <S : CharSequence> S.onEach(action: (Char) -> Unit): S {
/**
* Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun CharSequence.reduce(operation: (acc: Char, Char) -> Char): Char {
if (isEmpty())
@@ -1184,6 +1186,8 @@ public inline fun CharSequence.reduce(operation: (acc: Char, Char) -> Char): Cha
* to current accumulator value and each character with its index in the original char sequence.
* @param [operation] function that takes the index of a character, current accumulator value
* and the character itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun CharSequence.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char {
if (isEmpty())
@@ -1214,6 +1218,8 @@ public inline fun CharSequence.reduceOrNull(operation: (acc: Char, Char) -> Char
/**
* Accumulates value starting with last character and applying [operation] from right to left to each character and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun CharSequence.reduceRight(operation: (Char, acc: Char) -> Char): Char {
var index = lastIndex
@@ -1230,6 +1236,8 @@ public inline fun CharSequence.reduceRight(operation: (Char, acc: Char) -> Char)
* to each character with its index in the original char sequence and current accumulator value.
* @param [operation] function that takes the index of a character, the character itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
public inline fun CharSequence.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char {
var index = lastIndex
@@ -5480,6 +5480,8 @@ public inline fun UShortArray.none(predicate: (UShort) -> Boolean): Boolean {
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5496,6 +5498,8 @@ public inline fun UIntArray.reduce(operation: (acc: UInt, UInt) -> UInt): UInt {
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5512,6 +5516,8 @@ public inline fun ULongArray.reduce(operation: (acc: ULong, ULong) -> ULong): UL
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5528,6 +5534,8 @@ public inline fun UByteArray.reduce(operation: (acc: UByte, UByte) -> UByte): UB
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5547,6 +5555,8 @@ public inline fun UShortArray.reduce(operation: (acc: UShort, UShort) -> UShort)
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5566,6 +5576,8 @@ public inline fun UIntArray.reduceIndexed(operation: (index: Int, acc: UInt, UIn
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5585,6 +5597,8 @@ public inline fun ULongArray.reduceIndexed(operation: (index: Int, acc: ULong, U
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5604,6 +5618,8 @@ public inline fun UByteArray.reduceIndexed(operation: (index: Int, acc: UByte, U
* to current accumulator value and each element with its index in the original array.
* @param [operation] function that takes the index of an element, current accumulator value
* and the element itself and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5696,6 +5712,8 @@ public inline fun UShortArray.reduceOrNull(operation: (acc: UShort, UShort) -> U
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5712,6 +5730,8 @@ public inline fun UIntArray.reduceRight(operation: (UInt, acc: UInt) -> UInt): U
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5728,6 +5748,8 @@ public inline fun ULongArray.reduceRight(operation: (ULong, acc: ULong) -> ULong
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5744,6 +5766,8 @@ public inline fun UByteArray.reduceRight(operation: (UByte, acc: UByte) -> UByte
/**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5763,6 +5787,8 @@ public inline fun UShortArray.reduceRight(operation: (UShort, acc: UShort) -> US
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5783,6 +5809,8 @@ public inline fun UIntArray.reduceRightIndexed(operation: (index: Int, UInt, acc
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5803,6 +5831,8 @@ public inline fun ULongArray.reduceRightIndexed(operation: (index: Int, ULong, a
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -5823,6 +5853,8 @@ public inline fun UByteArray.reduceRightIndexed(operation: (index: Int, UByte, a
* to each element with its index in the original array and current accumulator value.
* @param [operation] function that takes the index of an element, the element itself
* and current accumulator value, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.reduceRight
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@@ -642,6 +642,24 @@ class Collections {
assertPrints(emptyMin, "null")
}
@Sample
fun reduce() {
val strings = listOf("a", "b", "c", "d")
assertPrints(strings.reduce { acc, string -> acc + string }, "abcd")
assertPrints(strings.reduceIndexed { index, acc, string -> acc + string + index }, "ab1c2d3")
assertFails { emptyList<Int>().reduce { _, _ -> 0 } }
}
@Sample
fun reduceRight() {
val strings = listOf("a", "b", "c", "d")
assertPrints(strings.reduceRight { string, acc -> acc + string }, "dcba")
assertPrints(strings.reduceRightIndexed { index, string, acc -> acc + string + index }, "dc2b1a0")
assertFails { emptyList<Int>().reduceRight { _, _ -> 0 } }
}
@Sample
fun reduceOrNull() {
val strings = listOf("a", "b", "c", "d")
@@ -646,6 +646,7 @@ object Aggregates : TemplateGroupBase() {
and the ${f.element} itself and calculates the next accumulator value.
"""
}
sample("samples.collections.Collections.Aggregates.reduce")
returns("T")
body {
"""
@@ -676,6 +677,7 @@ object Aggregates : TemplateGroupBase() {
}
typeParam("S")
typeParam("T : S")
sample("samples.collections.Collections.Aggregates.reduce")
returns("S")
body {
fun checkOverflow(value: String) = if (f == Sequences || f == Iterables) "checkIndexOverflow($value)" else value
@@ -719,6 +721,7 @@ object Aggregates : TemplateGroupBase() {
and current accumulator value, and calculates the next accumulator value.
"""
}
sample("samples.collections.Collections.Aggregates.reduceRight")
returns("T")
body {
"""
@@ -749,6 +752,7 @@ object Aggregates : TemplateGroupBase() {
and current accumulator value, and calculates the next accumulator value.
"""
}
sample("samples.collections.Collections.Aggregates.reduceRight")
typeParam("S")
typeParam("T : S")
returns("S")
@@ -790,6 +794,7 @@ object Aggregates : TemplateGroupBase() {
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}." }
sample("samples.collections.Collections.Aggregates.reduce")
returns("T")
body {
"""
@@ -811,6 +816,7 @@ object Aggregates : TemplateGroupBase() {
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}." }
sample("samples.collections.Collections.Aggregates.reduce")
typeParam("S")
typeParam("T : S")
returns("S")
@@ -910,6 +916,7 @@ object Aggregates : TemplateGroupBase() {
specialFor(ArraysOfUnsigned) { inlineOnly() }
doc { "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value." }
sample("samples.collections.Collections.Aggregates.reduceRight")
returns("T")
body {
"""
@@ -931,6 +938,7 @@ object Aggregates : TemplateGroupBase() {
} builder {
inline()
doc { "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value." }
sample("samples.collections.Collections.Aggregates.reduceRight")
typeParam("S")
typeParam("T : S")
returns("S")