Scan

Sometimes you want to pick up each value from an array, apply a specified operation on each element, and return a new array with new elements generated after the application of a specified operation. RxSwift offers the scan operator, which enables you to perform such actions. If you are familiar with the reduce(_:_:) Swift standard library method, you will find scan to be similar; scan starts with an initial seed value and is used to aggregate values just like reduce(_:_:), but there is a small difference—unlike reduce(_:_:), which only returns the final value, scan will return an Observable for each intermediate result along the way:

There is also a reduce operator in RxSwift that works just like the standard library reduce(_:_:)

Get Reactive Programming with Swift 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.