---
title: "reduce(_:_:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/reduce(_:_:)"
---

# reduce(_:_:)

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

## Declaration

```swift
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.

## Mentioned in

Receiving and Handling Events with Combine

## Return Value

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

## Discussion

Discussion Use reduce(_:_:) to collect a stream of elements and produce an accumulated value based on a closure you provide. In the following example, the 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"

## See Also

### Reducing elements

- [collect()](combine/publisher/collect().md)
- [collect(_:)](combine/publisher/collect(_:).md)
- [collect(_:options:)](combine/publisher/collect(_:options:).md)
- [Publishers.TimeGroupingStrategy](combine/publishers/timegroupingstrategy.md)
- [ignoreOutput()](combine/publisher/ignoreoutput().md)
- [tryReduce(_:_:)](combine/publisher/tryreduce(_:_:).md)
