Transforms elements from the upstream publisher by providing the current element to a closure along with the last value returned by the closure.
SDKs
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
Framework
- Combine
Declaration
func scan<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Output) -> T) -> Publishers.Scan<Publishers.Map<Upstream, Output>, T>
Parameters
initialResult
The previous result returned by the
next
closure.Partial Result 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
A publisher that transforms elements by applying a closure that receives its previous return value and the next element from the upstream publisher.
Discussion
let pub = (0...5)
.publisher()
.scan(0, { return $0 + $1 })
.sink(receiveValue: { print ("\($0)", terminator: " ") })
// Prints "0 1 3 6 10 15 ".