Swiftpack.co - Package - apple/swift-algorithms

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.


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

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.
  • product(_:_:): Iterates over all the pairs of two collections; equivalent to nested for-in loops.
  • cycled(), cycled(times:): Repeats the elements of a collection forever or a set number of times.

Subsetting operations

Partial sorting

Other useful operations

  • chunked(by:), chunked(on:), chunks(ofCount:): Eager and lazy operations that break a collection into chunks based on either a binary predicate or when the result of a projection changes or chunks of a given count.
  • indexed(): Iterate over tuples of a collection's indices and elements.
  • trimming(where:): Returns a slice by trimming elements from a collection's start and end.
  • slidingWindows(ofCount:): Breaks a collection into overlapping contiguous window subsequences where elements are slices from the original collection.
  • striding(by:): Returns every nth element of a collection.

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: "0.0.1"),

Because Algorithms is under active development, source-stability is only guaranteed within minor versions (e.g. between 0.0.3 and 0.0.4). If you don't want potentially source-breaking package updates, use this dependency specification instead:

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

Finally, include "Algorithms" as a dependency for your executable target:

let package = Package(
    // name, platforms, products, etc.
    dependencies: [
        .package(url: "https://github.com/apple/swift-algorithms", from: "0.0.1"),
        // other dependencies
    targets: [
        .target(name: "<target>", dependencies: [
            .product(name: "Algorithms", package: "swift-algorithms"),
        // other targets


Stars: 2237
Last commit: 13 hours ago


Swift Algorithms 0.0.2 - 2020-10-23T22:05:36


  • The rotate(at:) method has been updated to rotate(toStartAt:), with the old name deprecated.
  • The chained(with:) method has been changed to the free function chain(_:_:), with the old version deprecated.
  • Algorithms now uses RealModule from the swift-numerics package for its cross-platform elementary functions.
  • Sequence/collection wrapper types, like Permutations and Indexed, now have conformance to the lazy protocols, so that any following operations maintain their laziness.


  • Swift Algorithms now builds under SwiftPM on Windows.
  • A wide variety of errors, misspellings, and ommissions in the documentation and guides have been fixed.
  • Index/distance calculations for the Product2 and Chain types have been corrected.
  • Calling stablePartition(subrange:by:) now correctly uses the subrange's length instead of the whole collection.