Add foldIndexed and reduceIndexed groups of functions
- foldIndexed, foldRightIndexed, reduceIndexed and reduceRightIndexed have been added, in line with filterIndexed etc.; - Test cases added appropriately for the new functions.
This commit is contained in:
committed by
Ilya Gorbunov
parent
b3f390abe5
commit
d58efff974
@@ -1351,6 +1351,17 @@ public inline fun <T, R> Iterable<T>.fold(initial: R, operation: (R, T) -> R): R
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with [initial] value and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original collection.
|
||||
*/
|
||||
public inline fun <T, R> Iterable<T>.foldIndexed(initial: R, operation: (Int, R, T) -> R): R {
|
||||
var index = 0
|
||||
var accumulator = initial
|
||||
for (element in this) accumulator = operation(index++, accumulator, element)
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value.
|
||||
*/
|
||||
@@ -1363,6 +1374,20 @@ public inline fun <T, R> List<T>.foldRight(initial: R, operation: (T, R) -> R):
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with [initial] value and applying [operation] from right to left
|
||||
* to each element with its index in the original list and current accumulator value.
|
||||
*/
|
||||
public inline fun <T, R> List<T>.foldRightIndexed(initial: R, operation: (Int, T, R) -> R): R {
|
||||
var index = lastIndex
|
||||
var accumulator = initial
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given [action] on each element.
|
||||
*/
|
||||
@@ -1502,6 +1527,21 @@ public inline fun <S, T: S> Iterable<T>.reduce(operation: (S, T) -> S): S {
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying [operation] from left to right
|
||||
* to current accumulator value and each element with its index in the original collection.
|
||||
*/
|
||||
public inline fun <S, T: S> Iterable<T>.reduceIndexed(operation: (Int, S, T) -> S): S {
|
||||
val iterator = this.iterator()
|
||||
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
|
||||
var index = 1
|
||||
var accumulator: S = iterator.next()
|
||||
while (iterator.hasNext()) {
|
||||
accumulator = operation(index++, accumulator, iterator.next())
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value.
|
||||
*/
|
||||
@@ -1515,6 +1555,21 @@ public inline fun <S, T: S> List<T>.reduceRight(operation: (T, S) -> S): S {
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying [operation] from right to left
|
||||
* to each element with its index in the original list and current accumulator value.
|
||||
*/
|
||||
public inline fun <S, T: S> List<T>.reduceRightIndexed(operation: (Int, T, S) -> S): S {
|
||||
var index = lastIndex
|
||||
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
|
||||
var accumulator: S = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(index, get(index), accumulator)
|
||||
--index
|
||||
}
|
||||
return accumulator
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all values produced by [selector] function applied to each element in the collection.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user