# fold

Accumulates value starting with initial value and applying operation from left to right to current accumulator value and each element.

Returns the specified initial value if the array is empty.

#### Since Kotlin

1.0#### Parameters

__operation__

function that takes current accumulator value and an element, and calculates the next accumulator value.

Accumulates value starting with initial value and applying operation from left to right to current accumulator value and each element.

Returns the specified initial value if the collection is empty.

#### Since Kotlin

1.0#### Parameters

__operation__

function that takes current accumulator value and an element, and calculates the next accumulator value.

Accumulates value starting with initial value and applying operation from left to right to current accumulator value and each element.

Returns the specified initial value if the array is empty.

#### Since Kotlin

1.3#### Parameters

__operation__

function that takes current accumulator value and an element, and calculates the next accumulator value.

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in a new map. An initial value of accumulator is provided by initialValueSelector function.

#### Since Kotlin

1.1#### Return

a Map associating the key of each group with the result of accumulating the group elements.

#### Parameters

__initial__ValueSelector

a function that provides an initial value of accumulator for each group. It's invoked with parameters:

`key`

: the key of the group;`element`

: the first element being encountered in that group.

__operation__

a function that is invoked on each element with the following parameters:

`key`

: the key of the group this element belongs to;`accumulator`

: the current value of the accumulator of the group;`element`

: the element from the source being accumulated.

#### Samples

```
fun main() {
//sampleStart
val fruits = listOf("cherry", "blueberry", "citrus", "apple", "apricot", "banana", "coconut")
val evenFruits = fruits.groupingBy { it.first() }
.fold({ key, _ -> key to mutableListOf<String>() },
{ _, accumulator, element ->
accumulator.also { (_, list) -> if (element.length % 2 == 0) list.add(element) }
})
val sorted = evenFruits.values.sortedBy { it.first }
println(sorted) // [(a, []), (b, [banana]), (c, [cherry, citrus])]
//sampleEnd
}
```

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in a new map. An initial value of accumulator is the same initialValue for each group.

#### Since Kotlin

1.1#### Return

a Map associating the key of each group with the result of accumulating the group elements.

#### Parameters

__operation__

a function that is invoked on each element with the following parameters:

`accumulator`

: the current value of the accumulator of the group;`element`

: the element from the source being accumulated.

#### Samples

```
fun main() {
//sampleStart
val fruits = listOf("apple", "apricot", "banana", "blueberry", "cherry", "coconut")
// collect only even length Strings
val evenFruits = fruits.groupingBy { it.first() }
.fold(listOf<String>()) { acc, e -> if (e.length % 2 == 0) acc + e else acc }
println(evenFruits) // {a=[], b=[banana], c=[cherry]}
//sampleEnd
}
```