Swiftpack.co -  Package - apple/swift-algorithms
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
apple/swift-algorithms
Swift Algorithms is an open-source package of sequence and collection algorithms, along with their related types.
.package(url: "https://github.com/apple/swift-algorithms.git", from: "0.0.4")

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.
  • 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

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
    ]
)

GitHub

link
Stars: 2673
Last commit: Yesterday

Release Notes

Swift Algorithms 0.0.4
1 week ago

Additions

More new algorithms to join the party:

  • A lazy version of the standard library's two split methods. (#78)
  • firstNonNil(_:) returns the first non-nil element from an optional-generating transform. (#31)
  • uniquePermutations() skips duplicates when generating permutations of a collection. (#91)
  • The reductions methods return all the in-between states of reducing a sequence or collection. (#46)

Fixes

  • Methods and computed properties are more consistently marked as inlinable, resolving a performance regression.
  • The Stride type now efficiently calculates distances between positions, supported by the underlying collection.
  • Better test coverage and improved diagnostics for comparing sequences.
  • Fixed links and improved documentation.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco