Generic Instance Method

reduce(into:_:)

Returns the result of combining the elements of the sequence using the given closure.

Declaration

func reduce<Result>(into initialResult: Result, _ updateAccumulatingResult: (inout Result, Element) throws -> ()) rethrows -> Result

Parameters

initialResult

The value to use as the initial accumulating value.

updateAccumulatingResult

A closure that updates the accumulating value with an element of the sequence.

Return Value

The final accumulated value. If the sequence has no elements, the result is initialResult.

Discussion

Use the reduce(into:_:) method to produce a single value from the elements of an entire sequence. For example, you can use this method on an array of integers to filter adjacent equal entries or count frequencies.

This method is preferred over reduce(_:_:) for efficiency when the result is a copy-on-write type, for example an Array or a Dictionary.

The updateAccumulatingResult closure is called sequentially with a mutable accumulating value initialized to initialResult and each element of the sequence. This example shows how to build a dictionary of letter frequencies of a string.

let letters = "abracadabra"
let letterCount = letters.reduce(into: [:]) { counts, letter in
    counts[letter, default: 0] += 1
}
// letterCount == ["a": 5, "b": 2, "r": 2, "c": 1, "d": 1]

When letters.reduce(into:_:) is called, the following steps occur:

  1. The updateAccumulatingResult closure is called with the initial accumulating value—[:] in this case—and the first character of letters, modifying the accumulating value by setting 1 for the key "a".

  2. The closure is called again repeatedly with the updated accumulating value and each element of the sequence.

  3. When the sequence is exhausted, the accumulating value is returned to the caller.

If the sequence has no elements, updateAccumulatingResult is never executed and initialResult is the result of the call to reduce(into:_:).

Complexity: O(n), where n is the length of the sequence.

See Also

Transforming a Set

func map<T>((Element) -> T) -> [T]

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func compactMap<ElementOfResult>((Element) -> ElementOfResult?) -> [ElementOfResult]

Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

func flatMap<SegmentOfResult>((Element) -> SegmentOfResult) -> [SegmentOfResult.Element]

Returns an array containing the concatenated results of calling the given transformation with each element of this sequence.

func reduce<Result>(Result, (Result, Element) -> Result) -> Result

Returns the result of combining the elements of the sequence using the given closure.

func sorted() -> [Element]

Returns the elements of the sequence, sorted.

func sorted(by: (Element, Element) -> Bool) -> [Element]

Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.

func shuffled() -> [Element]

Returns the elements of the sequence, shuffled.

func shuffled<T>(using: inout T) -> [Element]

Returns the elements of the sequence, shuffled using the given generator as a source for randomness.

var lazy: LazySequence<Set<Element>>

A sequence containing the same elements as this sequence, but on which some operations, such as map and filter, are implemented lazily.