Commonly used sequence and collection algorithms for Swift

What's New

Swift Algorithms 1.0.0


Swift Algorithms is now source stable!


  • Most sequence and collection types have been renamed, following a more consistent naming structure:
    • The Lazy prefix was dropped.
    • Either a Sequence or Collection suffix was added depending on whether or not the type is unconditionally a collection.
    • The base name was derived from the name of the method that produces it, including an argument label to disambiguate if necessary.
  • Types that can only be produced from a lazy sequence chain now unconditionally conform to LazySequenceProtocol and wrap the base sequence instead of the lazy wrapper, making some return types slightly simpler.
    • e.g. [1, 2, 3].lazy.reductions(+) now returns ExclusiveReductionsSequence<[Int]>, not ExclusiveReductionsSequence<LazySequence<[Int]>>.
  • The generic parameters of the ExclusiveReductions type have been swapped, putting the base collection first and the result type second.
  • The Indices associated type of IndexedCollection now matches Base.Indices.


  • Previously deprecated type and method names have been removed:
    • The Chain type alias for Chain2Sequence
    • The chained(with:) method which was replaced with the chain(_:_:) free function
    • The LazyChunked and Chunked type aliases for ChunkedByCollection
    • The rotate(subrange:at:) and rotate(at:) methods which were renamed to rotate(subrange:toStartAt:) and rotate(toStartAt:) respectively


  • The StridingSequence and StridingCollection types now conditionally conform to LazySequenceProtocol, allowing the striding(by:) method to properly propagate laziness in a lazy sequence chain.
  • Fixed chunked(by:) to always compare two consecutive elements rather than each element with the first element of the current chunk. (#162)

Swift Algorithms

Swift Algorithms is an open-source package of sequence and collection algorithms, along with their related types.

Read more about the package, and the intent behind it, in the announcement on swift.org.


Combinations / permutations

Mutating algorithms

Combining collections

  • chain(_:_:): Concatenates two collections with the same element type.
  • cycled(), cycled(times:): Repeats the elements of a collection forever or a set number of times.
  • joined(by:): Concatenate sequences of sequences, using an element or sequence as a separator, or using a closure to generate each separator.
  • product(_:_:): Iterates over all the pairs of two collections; equivalent to nested for-in loops.

Subsetting operations

Partial sorting

Other useful operations

Adding Swift Algorithms as a Dependency

To use the Algorithms library in a SwiftPM project, add the following line to the dependencies in your Package.swift file:

.package(url: "https://github.com/apple/swift-algorithms", from: "1.0.0"),

Include "Algorithms" as a dependency for your executable target:

.target(name: "<target>", dependencies: [
    .product(name: "Algorithms", package: "swift-algorithms"),

Finally, add import Algorithms to your source code.

Source Stability

The Swift Numerics package is source stable; version numbers follow Semantic Versioning. Source breaking changes to public API can only land in a new major version.

The public API of version 1.0 of the swift-algorithms package consists of non-underscored declarations that are marked public in the Algorithms module. Interfaces that aren't part of the public API may continue to change in any release, including patch releases.

Future minor versions of the package may introduce changes to these rules as needed.

We'd like this package to quickly embrace Swift language and toolchain improvements that are relevant to its mandate. Accordingly, from time to time, we expect that new versions of this package will require clients to upgrade to a more recent Swift toolchain release. Requiring a new Swift release will only require a minor version bump.


  • Swift Tools 5.1.0
View More Packages from this Author


Last updated: Wed Oct 27 2021 21:34:38 GMT-0500 (GMT-05:00)