Reductions
Find the incremental values of a sequence “reduce” operation.
Overview
Call one of the reductions
methods when you want the result of a reduce
operation along with all of its intermediate values:
let exclusiveRunningTotal = (1...5).reductions(0, +)
print(exclusiveRunningTotal)
// prints [0, 1, 3, 6, 10, 15]
let inclusiveRunningTotal = (1...5).reductions(+)
print(inclusiveRunningTotal)
// prints [1, 3, 6, 10, 15]
Topics
func reductions((Element, Element) throws -> Element
) rethrows -> [Element] Returns an array containing the accumulated results of combining the elements of the sequence using the given closure.
func reductions<Result>(Result, (Result, Element) throws -> Result
) rethrows -> [Result] Returns an array containing the accumulated results of combining the elements of the sequence using the given closure.
func reductions<Result>(into: Result, (inout Result, Element) throws -> Void
) rethrows -> [Result] Returns an array containing the accumulated results of combining the elements of the sequence using the given closure.
func reductions(@escaping (Element, Element) -> Element
) -> InclusiveReductionsSequence<Elements> Returns a sequence containing the accumulated results of combining the elements of the sequence using the given closure.
func reductions<Result>(Result, @escaping (Result, Element) -> Result
) -> ExclusiveReductionsSequence<Elements, Result> Returns a sequence containing the accumulated results of combining the elements of the sequence using the given closure.
func reductions<Result>(into: Result, @escaping (inout Result, Element) -> Void
) -> ExclusiveReductionsSequence<Elements, Result> Returns a sequence containing the accumulated results of combining the elements of the sequence using the given closure.
Supporting Types
struct InclusiveReductionsSequence<Base>
struct ExclusiveReductionsSequence<Base, Result>
A sequence of applying a transform to the element of a sequence and the previously transformed result.
Deprecated Methods
DeprecatedScan
These methods are deprecated; use the
Read Morereductions
family of methods instead.