<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/reduce(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE6reduceyAA10PublishersO6ReduceVy_xqd__Gqd___qd__qd___6OutputQztctlF"
  },
  "title" : "reduce(_:_:)"
}
-->

# reduce(_:_:)

Applies a closure that collects each element of a stream and publishes a final result upon completion.

```
func reduce<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Self.Output) -> T) -> Publishers.Reduce<Self, T>
```

## Parameters

`initialResult`

The value that the closure receives the first time it’s called.

`nextPartialResult`

A closure that produces a new value by taking the previously-accumulated value and the next element it receives from the upstream publisher.

## Return Value

A publisher that applies the closure to all received elements and produces an accumulated value when the upstream publisher finishes. If [`reduce(_:_:)`](/documentation/Combine/Publisher/reduce(_:_:)) receives an error from the upstream publisher, the operator delivers it to the downstream subscriber, the publisher terminates and publishes no value.

## Discussion

Use [`reduce(_:_:)`](/documentation/Combine/Publisher/reduce(_:_:)) to collect a stream of elements and produce an accumulated value based on a closure you provide.

In the following example, the [`reduce(_:_:)`](/documentation/Combine/Publisher/reduce(_:_:)) operator collects all the integer values it receives from its upstream publisher:

```
let numbers = (0...10)
cancellable = numbers.publisher
    .reduce(0, { accum, next in accum + next })
    .sink { print("\($0)") }

// Prints: "55"
```

---

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)