Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/apple/swift-algorithms
Commonly used sequence and collection algorithms for Swift
https://github.com/apple/swift-algorithms
algorithm iterator itertools
Last synced: 5 days ago
JSON representation
Commonly used sequence and collection algorithms for Swift
- Host: GitHub
- URL: https://github.com/apple/swift-algorithms
- Owner: apple
- License: apache-2.0
- Created: 2020-10-01T04:00:52.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-05-07T19:53:18.000Z (6 months ago)
- Last Synced: 2024-05-18T15:42:12.464Z (6 months ago)
- Topics: algorithm, iterator, itertools
- Language: Swift
- Homepage:
- Size: 770 KB
- Stars: 5,726
- Watchers: 243
- Forks: 421
- Open Issues: 44
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-ios - swift-algorithms
- awesome-starts - apple/swift-algorithms - Swift Algorithms is an open-source package of sequence and collection algorithms, along with their related types. (Swift)
README
# Swift Algorithms
**Swift Algorithms** is an open-source package of sequence and collection algorithms, along with their related types.
## Overview
The Algorithms package provides a variety of sequence and collection operations, letting you cycle over a collection's elements, find combinations and permutations, create a random sample, and more.
For example, the package includes a group of "chunking" methods, each of which breaks a collection into consecutive subsequences. One version tests adjacent elements to find the breaking point between chunks — you can use it to quickly separate an array into ascending runs:
```swift
let numbers = [10, 20, 30, 10, 40, 40, 10, 20]
let chunks = numbers.chunked(by: { $0 <= $1 })
// [[10, 20, 30], [10, 40, 40], [10, 20]]
```Another version looks for a change in the transformation of each successive value. You can use that to separate a list of names into groups by the first character:
```swift
let names = ["Cassie", "Chloe", "Jasmine", "Jordan", "Taylor"]
let chunks = names.chunked(on: \.first)
// [["Cassie", "Chloe"], ["Jasmine", "Jordan"], ["Taylor"]]
```Explore more chunking methods and the remainder of the `Algorithms` package in the links below.
## Documentation
For API documentation, see the library's official documentation in Xcode or on the Web.
- [API documentation][docs]
- [Swift.org announcement][announcement]
- [API Proposals/Guides][guides]## 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:```swift
.package(url: "https://github.com/apple/swift-algorithms", from: "1.2.0"),
```Include `"Algorithms"` as a dependency for your executable target:
```swift
.target(name: "", dependencies: [
.product(name: "Algorithms", package: "swift-algorithms"),
]),
```Finally, add `import Algorithms` to your source code.
## Source Stability
The Swift Algorithms package is source stable; version numbers follow [Semantic Versioning](https://semver.org/). Source breaking changes to public API can only land in a new major version.
The public API 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.
[docs]: https://swiftpackageindex.com/apple/swift-algorithms/documentation/algorithms
[announcement]: https://swift.org/blog/swift-algorithms/
[guides]: https://github.com/apple/swift-algorithms/tree/main/Guides