KotlinCollection Opsfold
reduce

foldCollection Ops

groupBy

Accumulates value starting with initial value and applying operation to each element.

Syntax

collection.fold(initial) { acc, e -> }

Example

Enter values below to update the example in real time.

val
nums
listOf
fold
acc
println
val nums = listOf(1, 2, 3, 4, 5)
val sum = nums.fold(10) { acc, n -> acc + n }
println(sum) // 25