BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News The Swift Team Open-Sources Swift Algorithms

The Swift Team Open-Sources Swift Algorithms

This item in japanese

Bookmarks

Swift Algorithms is a new package including a number of sequence and collection algorithms that are going to fill a gap in Swift standard library, writes Apple engineer Nat Cook.

The Algorithms package includes a host of powerful, generic algorithms frequently found in other popular programming languages. We hope this new package will help people embrace algorithms, improving the correctness and performance of their code.

As an example of similar libraries available for other languages, Cook mentions itertools for Python and the C++ algorithms library. Specifically, Swift Algorithms will include generic algorithms over the Sequence and Collection protocols, and will not include classic algorithms such as sort algorithms nor algorithms over non-linear data structures.

The importance of choosing the right algorithms and data structures cannot be overstated and the lack of a standard library supporting this has in some cases reduced the attention Swift developers pay to it, as Dave Abrahams noticed in the "Embrace Algorithms" talk he gave at WWDC 2018.

We think the Algorithms package can help realize this goal by serving as a low-friction venue to build out new families of related algorithms—giving us an opportunity to iteratively explore the problem space and learn how different algorithms connect and interact—before graduating them into the standard library.

Swift Algorithms is still in its early stages and the number of algorithms it includes is still quite reduced. Namely, Swift Algorithms includes a Combinations generic type that computes combinations of a collection’s elements; a Permutations generic type to compute permutations of a collection’s elements, or of a subset of those elements; a product function to iterating over every pair of elements in two different collections; a set of chunked methods to break a collection into subsequences; a chain method to concatenate two collections with the same element type; a set of cycle methods to iterate over a collection; a uniqued method to strip repeated elements from a sequence or collection; a set of randomSample methods to carry through operations for randomly selecting k elements from a sequence or collection; an indexed method, which pairs elements of two collections; partition methods to perform a stable partition on mutable collections, and for finding the partitioning index in an already partitioned collection; and a rotating method to rotate the elements of a collection to new positions.

To start working with Swift Algorithms, you can add it as a dependency for your executable target in your Package.swift file:

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

Use of the library can be discussed in the Swift Algorithms Forum and new ideas can be submitted as issues.

Prior to Swift Algorithms, Swift developers could use the Swift Algorithm Club library, which is much more mature than the former and includes over 100 algorithms, not only related to sequences and collections but also including sorting, non-linear data strauctures, and more.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT