Github GitHub - apple/swift-algorithms: Swift Algorithms is an open-source packa...
source link: https://github.com/apple/swift-algorithms
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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
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.
Mutating algorithms
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.
Combining collections
chain(_:_:)
: Concatenates two collections with the same element type.product(_:_:)
: Iterates over all the pairs of two collections; equivalent to nestedfor
-in
loops.cycled()
,cycled(times:)
: Repeats the elements of a collection forever or a set number of times.
Subsetting operations
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.trimming(while:)
: Returns a slice by trimming elements from a collection's start and end.uniqued()
,uniqued(on:)
: The unique elements of a collection, preserving their order.
Partial sorting
min(count:)
,max(count:)
,min(count:sortedBy:)
,max(count:sortedBy:)
: Returns the smallest or largest elements of a collection, sorted by a predicate.
Other useful operations
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.indexed()
: Iterate over tuples of a collection's indices and elements.interspersed(with:)
: Place a value between every two elements of a sequence.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.compacted()
: Flatten thenil
s out of a sequence or collection.
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 ] )
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK