/swift-algorithms

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

Primary LanguageSwiftApache License 2.0Apache-2.0

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.

Contents

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

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.1.0"),
        // other dependencies
    ],
    targets: [
        .target(name: "<target>", dependencies: [
            .product(name: "Algorithms", package: "swift-algorithms"),
        ]),
        // other targets
    ]
)