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(ofCount:): Combinations of particular sizes of the elements in a collection.
permutations(ofCount:): Permutations of a particular size of the elements in a collection, or of the full collection.
uniquePermutations(ofCount:): Permutations of a collection's elements, skipping any duplicate permutations.
rotate(subrange:toStartAt:): In-place rotation of elements.
stablePartition(subrange:by:): A partition that preserves the relative order of the resulting prefix and suffix.
chain(_:_:): Concatenates two collections with the same element type.
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
compacted(): Drops the
nils from a sequence or collection, unwrapping the remaining elements.
partitioned(by:): Returns the elements in a sequence or collection that do and do not match a given predicate.
randomSample(count:using:): Randomly selects a specific number of elements from a collection.
randomStableSample(count:using:): Randomly selects a specific number of elements from a collection, preserving their original relative order.
striding(by:): Returns every nth element of a collection.
suffix(while:): Returns the suffix of a collection where all element pass a given predicate.
trimming(while:): Returns a slice by trimming elements from a collection's start, end, or both. The mutating
trim...methods trim a collection in place.
uniqued(on:): The unique elements of a collection, preserving their order.
minAndMax(by:): Returns the smallest and largest elements of a sequence.
max(count:sortedBy:): Returns the smallest or largest elements of a collection, sorted by a predicate.
adjacentPairs(): Lazily iterates over tuples of adjacent elements.
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.
firstNonNil(_:): Returns the first non-
nilresult from transforming a sequence's elements.
indexed(): Iterate over tuples of a collection's indices and elements.
interspersed(with:): Place a value between every two elements of a sequence.
partitioningIndex(where:): Returns the starting index of the partition of a collection that matches a predicate.
reductions(_:_:): Returns all the intermediate states of reducing the elements of a sequence or collection.
split(separator:maxSplits:omittingEmptySubsequences): Lazy versions of the Standard Library's eager operations that split sequences and collections into subsequences separated by the specified separator element.
windows(ofCount:): Breaks a collection into overlapping subsequences where elements are slices from the original collection.
To use the
Algorithms library in a SwiftPM project,
add the following line to the dependencies in your
.package(url: "https://github.com/apple/swift-algorithms", from: "1.0.0"),
"Algorithms" as a dependency for your executable target:
.target(name: "<target>", dependencies: [ .product(name: "Algorithms", package: "swift-algorithms"), ]),
import Algorithms to your source code.
The Swift Algorithms 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.
|Last commit: 1 week ago|
Swift Algorithms is now source stable!
Lazyprefix was dropped.
Collectionsuffix was added depending on whether or not the type is unconditionally a collection.
LazySequenceProtocoland wrap the base sequence instead of the lazy wrapper, making some return types slightly simpler.
[1, 2, 3].lazy.reductions(+)now returns
ExclusiveReductionstype have been swapped, putting the base collection first and the result type second.
Indicesassociated type of
Chaintype alias for
chained(with:)method which was replaced with the
Chunkedtype aliases for
rotate(at:)methods which were renamed to
StridingCollectiontypes now conditionally conform to
LazySequenceProtocol, allowing the
striding(by:)method to properly propagate laziness in a lazy sequence chain.
chunked(by:)to always compare two consecutive elements rather than each element with the first element of the current chunk. (#162)