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

# scan(_:_:)

Transforms elements from the upstream publisher by providing the current element to a closure along with the last value returned by the closure.

## Declaration

```swift
func scan<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Self.Output) -> T) -> Publishers.Scan<Self, T>
```

## Parameters

- `initialResult`: The previous result returned by the nextPartialResult closure.
- `nextPartialResult`: A closure that takes as its arguments the previous value returned by the closure and the next element emitted from the upstream publisher.

## Return Value

Return Value A publisher that transforms elements by applying a closure that receives its previous return value and the next element from the upstream publisher.

## Discussion

Discussion Use scan(_:_:) to accumulate all previously-published values into a single value, which you then combine with each newly-published value. The following example logs a running total of all values received from the sequence publisher. let range = (0...5) cancellable = range.publisher     .scan(0) { return $0 + $1 }     .sink { print ("\($0)", terminator: " ") }  // Prints: "0 1 3 6 10 15 ".

## See Also

### Mapping elements

- [map(_:)](combine/publisher/map(_:)-99evh.md)
- [tryMap(_:)](combine/publisher/trymap(_:).md)
- [mapError(_:)](combine/publisher/maperror(_:).md)
- [replaceNil(with:)](combine/publisher/replacenil(with:).md)
- [tryScan(_:_:)](combine/publisher/tryscan(_:_:).md)
- [setFailureType(to:)](combine/publisher/setfailuretype(to:).md)
