🐦 Kotlin
Intermediate
What is the reduce function in Kotlin?
Answer
reduce is similar to fold but uses the first element of the collection as the initial accumulator value instead of requiring you to provide one. Example: val sum = numbers.reduce { acc, n -> acc + n }. Because the first element is used as the starting value, reduce throws an UnsupportedOperationException on empty collections. When the collection might be empty, prefer fold with an explicit initial value (like 0 or an empty string). Use reduceRight to process elements from right to left.
Previous
What is the @JvmOverloads annotation in Kotlin?
Next
What is the partition function in Kotlin?