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