Files
swift-async-algorithms/Guides/Compacted.md
T
4d4022e91b Add a whole bunch more to the guides (#64)
* Rebase guides WIP to avoid source changes

* Add prose for debounce and throttle

* Add an alterantive for debounce

* Starts to prose for reductions

* Add effect listings for all types

* Add some examples for reductions

* Update Guides/Channel.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Channel.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Channel.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Channel.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Collections.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Throttle.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Throttle.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Throttle.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Sources/AsyncAlgorithms/AsyncInclusiveReductionsSequence.swift

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Adjust events to elements for channels

* Add a bit more detail to the README and a start to the guide for Task.select

* Chunked updates

* Add a getting started section to the README

* Add some more prose for Channel

* Update Guides/RemoveDuplicates.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Apply suggestions from code review

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Add some brief prose for Compacted

* reductions could be named scan

* spelling and correctness fixes

* Apply @kperryua's suggestion for changing the intro of Compacted

* Add some brief prose for Timer

* Apply suggestions from code review for README

Co-authored-by: Tony Parker <anthony.parker@apple.com>

* Add some brief prose for RemoveDuplicates

* Fleshing out of `Joined`

Co-authored-by: Chris Adamson <invalidname@gmail.com>
Co-authored-by: Kevin Perry <kperry@apple.com>
Co-authored-by: Tony Parker <anthony.parker@apple.com>
2022-03-17 18:40:14 -07:00

2.4 KiB

Compacted

[ Source | Tests ]

Introduction

Just as it is common for Sequence types that contain optional values to need to .compactMap { $0 }, AsyncSequence types have the same use cases. This common task means that the type must employ a closure to test the optional value. This can be done more efficeintly for both execution performance as well as API efficiency of typing.

Proposed Solution

Similar to the Swift Algorithms package we propose that a new method be added to AsyncSequence to fit this need.

extension AsyncSequence {
  public func compacted<Unwrapped>() -> AsyncCompactedSequence<Self, Unwrapped>
    where Element == Unwrapped?
}

This is equivalent to writing .compactMap { $0 } from a behavioral standpoint but is easier to reason about and is more efficient since it does not need to execute or store a closure.

Detailed Design

The AsyncCompactedSequence type from an effects standpoint works just like AsyncCompactMapSequence. When the base asynchronous sequence throws, the iteration of AsyncCompactedSequence can throw. Likewise if the base does not throw then the iteration of AsyncCompactedSequence does not throw. This type is conditionally Sendable when the base, base element, and base iterator are `Sendable.

public struct AsyncCompactedSequence<Base: AsyncSequence, Element>: AsyncSequence
  where Base.Element == Element? {

  public struct Iterator: AsyncIteratorProtocol {
    public mutating func next() async rethrows -> Element?
  }

  public func makeAsyncIterator() -> Iterator {
    Iterator(base.makeAsyncIterator())
  }
}

extension AsyncCompactedSequence: Sendable 
  where 
    Base: Sendable, Base.Element: Sendable, 
    Base.AsyncIterator: Sendable { }
  
extension AsyncCompactedSequence.Iterator: Sendable 
  where 
    Base: Sendable, Base.Element: Sendable, 
    Base.AsyncIterator: Sendable { }

Credits/Inspiration

This transformation function is a direct analog to the synchronous version defined in the Swift Algorithms package