<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/EmptyCollection/reduce(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:STsE6reduceyqd__qd___qd__qd___7ElementQztKXEtKlF::SYNTHESIZED::s:s15EmptyCollectionV"
  },
  "title" : "reduce(_:_:)"
}
-->

# reduce(_:_:)

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

```
func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Self.Element) throws -> Result) rethrows -> Result
```

## Parameters

`initialResult`

The value to use as the initial accumulating value.
`initialResult` is passed to `nextPartialResult` the first time the
closure is executed.

`nextPartialResult`

A closure that combines an accumulating value and
an element of the sequence into a new accumulating value, to be used
in the next call of the `nextPartialResult` closure or returned to
the caller.

## Return Value

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

## Discussion

Use the `reduce(_:_:)` method to produce a single value from the elements
of an entire sequence. For example, you can use this method on an array
of numbers to find their sum or product.

The `nextPartialResult` closure is called sequentially with an
accumulating value initialized to `initialResult` and each element of
the sequence. This example shows how to find the sum of an array of
numbers.

```
let numbers = [1, 2, 3, 4]
let numberSum = numbers.reduce(0, { x, y in
    x + y
})
// numberSum == 10
```

When `numbers.reduce(_:_:)` is called, the following steps occur:

1. The `nextPartialResult` closure is called with `initialResult`—`0`
   in this case—and the first element of `numbers`, returning the sum:
   `1`.
2. The closure is called again repeatedly with the previous call’s return
   value and each element of the sequence.
3. When the sequence is exhausted, the last value returned from the
   closure is returned to the caller.

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

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

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)