Kotlin Set runningReduceIndexed()
Syntax & Examples


Set.runningReduceIndexed() extension function

The runningReduceIndexed() extension function in Kotlin returns a list containing successive accumulation values generated by applying an operation from left to right to each element, its index in the original collection, and the current accumulator value that starts with the first element of this collection.


Syntax of Set.runningReduceIndexed()

The syntax of Set.runningReduceIndexed() extension function is:

fun <S, T : S> Set<T>.runningReduceIndexed(operation: (index: Int, acc: S, T) -> S): List<S>

This runningReduceIndexed() extension function of Set returns a list containing successive accumulation values generated by applying operation from left to right to each element, its index in the original collection and current accumulator value that starts with the first element of this collection.

Parameters

ParameterOptional/RequiredDescription
operationrequiredA function that takes the current index, the current accumulator value, and an element, and returns the new accumulator value.

Return Type

Set.runningReduceIndexed() returns value of type List.



✐ Examples

1 Running sum of elements in a set of integers with index

Using runningReduceIndexed() to calculate the running sum of elements in a set of integers while considering the index.

For example,

  1. Create a set of integers.
  2. Use runningReduceIndexed() with an operation that adds each element to the accumulator while considering the index.
  3. Print the resulting list of running sums.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    val runningSums = numbers.runningReduceIndexed { index, acc, num -> acc + num + index }
    println(runningSums)
}

Output

[1, 4, 9, 16, 25]

2 Running concatenation of strings in a set with index

Using runningReduceIndexed() to calculate the running concatenation of strings in a set while considering the index.

For example,

  1. Create a set of strings.
  2. Use runningReduceIndexed() with an operation that concatenates each string to the accumulator while considering the index.
  3. Print the resulting list of running concatenations.

Kotlin Program

fun main() {
    val strings = setOf("Kotlin", "is", "fun")
    val runningConcat = strings.runningReduceIndexed { index, acc, str -> "$acc $index:$str" }
    println(runningConcat)
}

Output

[Kotlin, Kotlin 1:is, Kotlin 1:is 2:fun]

3 Running product of elements in a set of integers with index

Using runningReduceIndexed() to calculate the running product of elements in a set of integers while considering the index.

For example,

  1. Create a set of integers.
  2. Use runningReduceIndexed() with an operation that multiplies each element to the accumulator while considering the index.
  3. Print the resulting list of running products.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4)
    val runningProducts = numbers.runningReduceIndexed { index, acc, num -> acc * (num + index) }
    println(runningProducts)
}

Output

[1, 3, 15, 75]

Summary

In this Kotlin tutorial, we learned about runningReduceIndexed() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.