These guides describe the design and intention behind the APIs included in the Algorithms
library. For further reading, see the announcement on swift.org and the official documentation.
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(toStartAt:)
,rotate(subrange:toStartAt:)
: In-place rotation of elements.stablePartition(by:)
,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()
,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 nestedfor
-in
loops.
compacted()
: Drops thenil
s 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:)
,randomSample(count:using:)
: Randomly selects a specific number of elements from a collection.randomStableSample(count:)
,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.trimmingPrefix(while:)
,trimmingSuffix(while)
,trimming(while:)
: Returns a slice by trimming elements from a collection's start, end, or both. The mutatingtrim...
methods trim a collection in place.uniqued()
,uniqued(on:)
: The unique elements of a collection, preserving their order.minAndMax()
,minAndMax(by:)
: Returns the smallest and largest elements of a sequence.
min(count:)
,max(count:)
,min(count:sortedBy:)
,max(count:sortedBy:)
: Returns the smallest or largest elements of a collection, sorted by a predicate.
adjacentPairs()
: Lazily iterates over tuples of adjacent elements.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.firstNonNil(_:)
: Returns the first non-nil
result from transforming a sequence's elements.grouped(by:)
: Group up elements using the given closure, returning a Dictionary of those groups, keyed by the results of the closure.indexed()
: Iterate over tuples of a collection's indices and elements.interspersed(with:)
: Place a value between every two elements of a sequence.keyed(by:)
,keyed(by:resolvingConflictsBy:)
: Returns a Dictionary that associates elements of a sequence with the keys returned by the given closure.partitioningIndex(where:)
: Returns the starting index of the partition of a collection that matches a predicate.reductions(_:)
,reductions(_:_:)
: Returns all the intermediate states of reducing the elements of a sequence or collection.split(maxSplits:omittingEmptySubsequences:whereSeparator)
,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.