Connect with us

Code

Swift Async Algorithms – Asynchronous sequence and advanced algorithms

Swift Async Algorithms is an open-source package of asynchronous sequence and advanced algorithms that involve concurrency, along with their related types.

This package has three main goals:

  • First-class integration with async/await
  • Provide a home for time-based algorithms
  • Be cross-platform and open source

Motivation

AsyncAlgorithms is a package for algorithms that work with values over time. That includes those primarily about time, like debounce and throttle, but also algorithms about order like combineLatest and merge. Operations that work with multiple inputs (like zip does on Sequence) can be surprisingly complex to implement, with subtle behaviors and many edge cases to consider. A shared package can get these details correct, with extensive testing and documentation, for the benefit of all Swift apps.

The foundation for AsyncAlgorithms was included in Swift 5.5 from AsyncSequence. Swift 5.5 also brings the ability to use a natural for/in loop with await to process the values in an AsyncSequence and Sequence-equivalent API like map and filter. Structured concurrency allows us to write code where intermediate state is simply a local variable, try can be used directly on functions that throw, and generally treat the logic for asynchronous code similar to that of synchronous code.

This package is the home for these APIs. Development and API design take place on GitHub and the Swift Forums.

Contents

Combining asynchronous sequences

  • chain(_:...): Concatenates two or more asynchronous sequences with the same element type.
  • combineLatest(_:...): Combines two or more asynchronous sequences into an asynchronous sequence producing a tuple of elements from those base asynchronous sequences that updates when any of the base sequences produce a value.
  • merge(_:...): Merges two or more asynchronous sequence into a single asynchronous sequence producing the elements of all of the underlying asynchronous sequences.
  • zip(_:...): Creates an asynchronous sequence of pairs built out of underlying asynchronous sequences.
  • joined(separator:): Concatenated elements of an asynchronous sequence of asynchronous sequences, inserting the given separator between each element.

Creating asynchronous sequences

  • async: Create an asynchronous sequence composed from a synchronous sequence.
  • AsyncChannel: An asynchronous sequence with back pressure sending semantics.
  • AsyncThrowingChannel: An asynchronous sequence with back pressure sending semantics that can emit failures.

Performance optimized asynchronous iterators

  • AsyncBufferedByteIterator: A highly efficient iterator useful for iterating byte sequences derived from asynchronous read functions.

Other useful asynchronous sequences

Asynchronous Sequences that transact in time

Obtaining all values from an asynchronous sequence

Task management

  • Task.select(_:): Determine the first task to complete of a sequence of tasks.

Effects

Each algorithm has specific behavioral effects. For throwing effects these can either be if the sequence throws, does not throw, or rethrows errors. Sendability effects in some asynchronous sequences are conditional whereas others require the composed parts to all be sendable to satisfy a requirement of Sendable. The effects are listed here.

Swift Async Algorithms on GitHub: https://github.com/SwiftKickMobile/SwiftMessages
Platform: iOS/Swift
⭐️: 649

Advertisement

Trending