Kotlin Set reduceIndexedOrNull()
Syntax & Examples
Set.reduceIndexedOrNull() extension function
The reduceIndexedOrNull() extension function in Kotlin accumulates a value starting with the first element and applying an operation from left to right to the current accumulator value and each element with its index in the original collection. If the collection is empty, it returns null.
Syntax of Set.reduceIndexedOrNull()
The syntax of Set.reduceIndexedOrNull() extension function is:
fun <S, T : S> Set<T>.reduceIndexedOrNull(operation: (index: Int, acc: S, T) -> S): S?
This reduceIndexedOrNull() extension function of Set 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.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
operation | required | A function that takes the current index, the current accumulator value, and an element, and returns the new accumulator value. |
Return Type
Set.reduceIndexedOrNull() returns value of type S?
.
✐ Examples
1 Summing elements in a set of integers with index or returning null if empty
Using reduceIndexedOrNull() to sum the elements in a set of integers while considering the index, or return null if the set is empty.
For example,
- Create a set of integers.
- Use reduceIndexedOrNull() with an operation that adds each element to the accumulator while considering the index.
- Print the resulting sum or null.
Kotlin Program
fun main() {
val numbers = setOf(1, 2, 3, 4, 5)
val sum = numbers.reduceIndexedOrNull { index, acc, num -> acc + num + index }
println(sum)
}
Output
25
2 Concatenating strings in a set with index or returning null if empty
Using reduceIndexedOrNull() to concatenate the strings in a set while considering the index, or return null if the set is empty.
For example,
- Create a set of strings.
- Use reduceIndexedOrNull() with an operation that concatenates each string to the accumulator while considering the index.
- Print the resulting concatenated string or null.
Kotlin Program
fun main() {
val strings = setOf("Kotlin", "is", "fun")
val result = strings.reduceIndexedOrNull { index, acc, str -> "$acc $index:$str" }
println(result)
}
Output
Kotlin 1:is 2:fun
3 Calculating the product of elements in a set of integers with index or returning null if empty
Using reduceIndexedOrNull() to calculate the product of the elements in a set of integers while considering the index, or return null if the set is empty.
For example,
- Create a set of integers.
- Use reduceIndexedOrNull() with an operation that multiplies each element to the accumulator while considering the index.
- Print the resulting product or null.
Kotlin Program
fun main() {
val numbers = setOf(1, 2, 3, 4)
val product = numbers.reduceIndexedOrNull { index, acc, num -> acc * (num + index) }
println(product)
}
Output
288
4 Handling an empty set with reduceIndexedOrNull()
Using reduceIndexedOrNull() to handle an empty set and return null.
For example,
- Create an empty set of integers.
- Use reduceIndexedOrNull() with an operation that adds each element to the accumulator while considering the index.
- Print the resulting value or null.
Kotlin Program
fun main() {
val emptySet = emptySet<Int>()
val result = emptySet.reduceIndexedOrNull { index, acc, num -> acc + num + index }
println(result)
}
Output
null
Summary
In this Kotlin tutorial, we learned about reduceIndexedOrNull() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.