reductions(into:_:)

Returns a sequence containing the accumulated results of combining the elements of the sequence using the given closure.

Reductions.swift:68
func reductions<Result>(into initial: Result, _ transform: @escaping (inout Result, Element) -> Void) -> ExclusiveReductionsSequence<Elements, Result>

Parameters

initial

The value to use as the initial value.

transform

A closure that combines the previously reduced result and the next element in the receiving sequence.

Returns

A sequence of the initial value followed by the reduced elements.

This can be seen as applying the reduce function to each element and providing the initial value followed by these results as a sequence.

let runningTotal = [1, 2, 3, 4].lazy.reductions(into: 0, +)
print(Array(runningTotal))

// prints [0, 1, 3, 6, 10]