Skip to content

Commit f6919df

Browse files
committed
Documentation improvements
- Restructure the project README, moving the list of guides into a README in the `Guides` directory - Curate the `keyed(by:)` and `grouped(by:)` methods
1 parent f2fd047 commit f6919df

File tree

5 files changed

+102
-45
lines changed

5 files changed

+102
-45
lines changed

CHANGELOG.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This project follows semantic versioning.
88

99
## [Unreleased]
1010

11+
*No new changes.*
12+
1113
## [1.2.0] - 2023-11-14
1214

1315
### Additions
@@ -31,6 +33,8 @@ This project follows semantic versioning.
3133

3234
- Documentation and performance improvements. ([#202], [#210])
3335

36+
The 1.2.0 release includes contributions from [amomchilov], [mattyoung], [natecook1000], [stephentyrone], and [ttsugriy].
37+
Thank you!
3438

3539
---
3640

@@ -334,7 +338,8 @@ This changelog's format is based on [Keep a Changelog](https://keepachangelog.co
334338

335339
<!-- Link references for releases -->
336340

337-
[Unreleased]: https://github.com/apple/swift-algorithms/compare/1.1.0...HEAD
341+
[Unreleased]: https://github.com/apple/swift-algorithms/compare/1.2.0...HEAD
342+
[1.2.0]: https://github.com/apple/swift-algorithms/compare/1.1.0...1.2.0
338343
[1.1.0]: https://github.com/apple/swift-algorithms/compare/1.0.0...1.1.0
339344
[1.0.0]: https://github.com/apple/swift-algorithms/compare/0.2.1...1.0.0
340345
[0.2.1]: https://github.com/apple/swift-algorithms/compare/0.2.0...0.2.1
@@ -383,6 +388,7 @@ This changelog's format is based on [Keep a Changelog](https://keepachangelog.co
383388

384389
<!-- Link references for contributors -->
385390

391+
[amomchilov]: https://github.com/apple/swift-algorithms/commits?author=amomchilov
386392
[AustinConlon]: https://github.com/apple/swift-algorithms/commits?author=AustinConlon
387393
[benrimmington]: https://github.com/apple/swift-algorithms/commits?author=benrimmington
388394
[bjhomer]: https://github.com/apple/swift-algorithms/commits?author=bjhomer
@@ -420,3 +426,4 @@ This changelog's format is based on [Keep a Changelog](https://keepachangelog.co
420426
[stephentyrone]: https://github.com/apple/swift-algorithms/commits?author=stephentyrone
421427
[timvermeulen]: https://github.com/apple/swift-algorithms/commits?author=timvermeulen
422428
[toddthomas]: https://github.com/apple/swift-algorithms/commits?author=toddthomas
429+
[ttsugriy]: https://github.com/apple/swift-algorithms/commits?author=ttsugriy

Guides/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Guides
2+
3+
These guides describe the design and intention behind the APIs included in the `Algorithms` library. For further reading, see the [announcement on swift.org](https://swift.org/blog/swift-algorithms/) and the [official documentation](https://swiftpackageindex.com/apple/swift-algorithms/documentation/algorithms).
4+
5+
## Contents
6+
7+
#### Combinations / permutations
8+
9+
- [`combinations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Combinations.md): Combinations of particular sizes of the elements in a collection.
10+
- [`permutations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Permutations.md): Permutations of a particular size of the elements in a collection, or of the full collection.
11+
- [`uniquePermutations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Permutations.md): Permutations of a collection's elements, skipping any duplicate permutations.
12+
13+
#### Mutating algorithms
14+
15+
- [`rotate(toStartAt:)`, `rotate(subrange:toStartAt:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Rotate.md): In-place rotation of elements.
16+
- [`stablePartition(by:)`, `stablePartition(subrange:by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Partition.md): A partition that preserves the relative order of the resulting prefix and suffix.
17+
18+
#### Combining collections
19+
20+
- [`chain(_:_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Chain.md): Concatenates two collections with the same element type.
21+
- [`cycled()`, `cycled(times:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Cycle.md): Repeats the elements of a collection forever or a set number of times.
22+
- [`joined(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Joined.md): Concatenate sequences of sequences, using an element or sequence as a separator, or using a closure to generate each separator.
23+
- [`product(_:_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Product.md): Iterates over all the pairs of two collections; equivalent to nested `for`-`in` loops.
24+
25+
#### Subsetting operations
26+
27+
- [`compacted()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Compacted.md): Drops the `nil`s from a sequence or collection, unwrapping the remaining elements.
28+
- [`partitioned(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Partition.md): Returns the elements in a sequence or collection that do and do not match a given predicate.
29+
- [`randomSample(count:)`, `randomSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection.
30+
- [`randomStableSample(count:)`, `randomStableSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection, preserving their original relative order.
31+
- [`striding(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Stride.md): Returns every nth element of a collection.
32+
- [`suffix(while:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Suffix.md): Returns the suffix of a collection where all element pass a given predicate.
33+
- [`trimmingPrefix(while:)`, `trimmingSuffix(while)`, `trimming(while:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Trim.md): Returns a slice by trimming elements from a collection's start, end, or both. The mutating `trim...` methods trim a collection in place.
34+
- [`uniqued()`, `uniqued(on:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md): The unique elements of a collection, preserving their order.
35+
- [`minAndMax()`, `minAndMax(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/MinMax.md): Returns the smallest and largest elements of a sequence.
36+
37+
#### Partial sorting
38+
39+
- [`min(count:)`, `max(count:)`, `min(count:sortedBy:)`, `max(count:sortedBy:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/MinMax.md): Returns the smallest or largest elements of a collection, sorted by a predicate.
40+
41+
#### Other useful operations
42+
43+
- [`adjacentPairs()`](https://github.com/apple/swift-algorithms/blob/main/Guides/AdjacentPairs.md): Lazily iterates over tuples of adjacent elements.
44+
- [`chunked(by:)`, `chunked(on:)`, `chunks(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Chunked.md): 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.
45+
- [`firstNonNil(_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/FirstNonNil.md): Returns the first non-`nil` result from transforming a sequence's elements.
46+
- [`grouped(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Grouped.md): Group up elements using the given closure, returning a Dictionary of those groups, keyed by the results of the closure.
47+
- [`indexed()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Indexed.md): Iterate over tuples of a collection's indices and elements.
48+
- [`interspersed(with:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Intersperse.md): Place a value between every two elements of a sequence.
49+
- [`keyed(by:)`, `keyed(by:resolvingConflictsBy:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Keyed.md): Returns a Dictionary that associates elements of a sequence with the keys returned by the given closure.
50+
- [`partitioningIndex(where:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Partition.md): Returns the starting index of the partition of a collection that matches a predicate.
51+
- [`reductions(_:)`, `reductions(_:_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Reductions.md): Returns all the intermediate states of reducing the elements of a sequence or collection.
52+
- [`split(maxSplits:omittingEmptySubsequences:whereSeparator)`, `split(separator:maxSplits:omittingEmptySubsequences)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Split.md): Lazy versions of the Standard Library's eager operations that split sequences and collections into subsequences separated by the specified separator element.
53+
- [`windows(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Windows.md): Breaks a collection into overlapping subsequences where elements are slices from the original collection.

README.md

+26-44
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,43 @@
22

33
**Swift Algorithms** is an open-source package of sequence and collection algorithms, along with their related types.
44

5-
Read more about the package, and the intent behind it, in the [announcement on swift.org](https://swift.org/blog/swift-algorithms/).
5+
## Overview
66

7-
## Contents
7+
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.
88

9-
#### Combinations / permutations
9+
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:
1010

11-
- [`combinations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Combinations.md): Combinations of particular sizes of the elements in a collection.
12-
- [`permutations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Permutations.md): Permutations of a particular size of the elements in a collection, or of the full collection.
13-
- [`uniquePermutations(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Permutations.md): Permutations of a collection's elements, skipping any duplicate permutations.
14-
15-
#### Mutating algorithms
16-
17-
- [`rotate(toStartAt:)`, `rotate(subrange:toStartAt:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Rotate.md): In-place rotation of elements.
18-
- [`stablePartition(by:)`, `stablePartition(subrange:by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Partition.md): A partition that preserves the relative order of the resulting prefix and suffix.
19-
20-
#### Combining collections
21-
22-
- [`chain(_:_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Chain.md): Concatenates two collections with the same element type.
23-
- [`cycled()`, `cycled(times:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Cycle.md): Repeats the elements of a collection forever or a set number of times.
24-
- [`joined(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Joined.md): Concatenate sequences of sequences, using an element or sequence as a separator, or using a closure to generate each separator.
25-
- [`product(_:_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Product.md): Iterates over all the pairs of two collections; equivalent to nested `for`-`in` loops.
11+
```swift
12+
let numbers = [10, 20, 30, 10, 40, 40, 10, 20]
13+
let chunks = numbers.chunked(by: { $0 <= $1 })
14+
// [[10, 20, 30], [10, 40, 40], [10, 20]]
15+
```
2616

27-
#### Subsetting operations
17+
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:
2818

29-
- [`compacted()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Compacted.md): Drops the `nil`s from a sequence or collection, unwrapping the remaining elements.
30-
- [`partitioned(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Partition.md): Returns the elements in a sequence or collection that do and do not match a given predicate.
31-
- [`randomSample(count:)`, `randomSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection.
32-
- [`randomStableSample(count:)`, `randomStableSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection, preserving their original relative order.
33-
- [`striding(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Stride.md): Returns every nth element of a collection.
34-
- [`suffix(while:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Suffix.md): Returns the suffix of a collection where all element pass a given predicate.
35-
- [`trimmingPrefix(while:)`, `trimmingSuffix(while)`, `trimming(while:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Trim.md): Returns a slice by trimming elements from a collection's start, end, or both. The mutating `trim...` methods trim a collection in place.
36-
- [`uniqued()`, `uniqued(on:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md): The unique elements of a collection, preserving their order.
37-
- [`minAndMax()`, `minAndMax(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/MinMax.md): Returns the smallest and largest elements of a sequence.
19+
```swift
20+
let names = ["Cassie", "Chloe", "Jasmine", "Jordan", "Taylor"]
21+
let chunks = names.chunked(on: \.first)
22+
// [["Cassie", "Chloe"], ["Jasmine", "Jordan"], ["Taylor"]]
23+
```
3824

39-
#### Partial sorting
25+
Explore more chunking methods and the remainder of the `Algorithms` package in the links below.
4026

41-
- [`min(count:)`, `max(count:)`, `min(count:sortedBy:)`, `max(count:sortedBy:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/MinMax.md): Returns the smallest or largest elements of a collection, sorted by a predicate.
27+
## Documentation
4228

43-
#### Other useful operations
29+
For API documentation, see the library's official documentation in Xocde or on the Web.
4430

45-
- [`adjacentPairs()`](https://github.com/apple/swift-algorithms/blob/main/Guides/AdjacentPairs.md): Lazily iterates over tuples of adjacent elements.
46-
- [`chunked(by:)`, `chunked(on:)`, `chunks(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Chunked.md): 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.
47-
- [`firstNonNil(_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/FirstNonNil.md): Returns the first non-`nil` result from transforming a sequence's elements.
48-
- [`grouped(by:)](https://github.com/apple/swift-algorithms/blob/main/Guides/Grouped.md): Group up elements using the given closure, returning a Dictionary of those groups, keyed by the results of the closure.
49-
- [`indexed()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Indexed.md): Iterate over tuples of a collection's indices and elements.
50-
- [`interspersed(with:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Intersperse.md): Place a value between every two elements of a sequence.
51-
- [`keyed(by:)`, `keyed(by:resolvingConflictsBy:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Keyed.md): Returns a Dictionary that associates elements of a sequence with the keys returned by the given closure.
52-
- [`partitioningIndex(where:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Partition.md): Returns the starting index of the partition of a collection that matches a predicate.
53-
- [`reductions(_:)`, `reductions(_:_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Reductions.md): Returns all the intermediate states of reducing the elements of a sequence or collection.
54-
- [`split(maxSplits:omittingEmptySubsequences:whereSeparator)`, `split(separator:maxSplits:omittingEmptySubsequences)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Split.md): Lazy versions of the Standard Library's eager operations that split sequences and collections into subsequences separated by the specified separator element.
55-
- [`windows(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Windows.md): Breaks a collection into overlapping subsequences where elements are slices from the original collection.
31+
- [API documentation][docs]
32+
- [Swift.org announcement][announcement]
33+
- [API Proposals/Guides][guides]
5634

5735
## Adding Swift Algorithms as a Dependency
5836

5937
To use the `Algorithms` library in a SwiftPM project,
6038
add the following line to the dependencies in your `Package.swift` file:
6139

6240
```swift
63-
.package(url: "https://github.com/apple/swift-algorithms", from: "1.0.0"),
41+
.package(url: "https://github.com/apple/swift-algorithms", from: "1.2.0"),
6442
```
6543

6644
Include `"Algorithms"` as a dependency for your executable target:
@@ -77,8 +55,12 @@ Finally, add `import Algorithms` to your source code.
7755

7856
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.
7957

80-
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.
58+
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.
8159

8260
Future minor versions of the package may introduce changes to these rules as needed.
8361

8462
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.
63+
64+
[docs]: https://swiftpackageindex.com/apple/swift-algorithms/documentation/algorithms
65+
[announcement]: https://swift.org/blog/swift-algorithms/
66+
[guides]: https://github.com/apple/swift-algorithms/tree/main/Guides

Sources/Algorithms/Documentation.docc/Algorithms.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Explore more chunking methods and the remainder of the Algorithms package, group
3333
- <doc:Joining>
3434
- <doc:Extending>
3535
- <doc:Trimming>
36+
- <doc:Keying>
3637
- <doc:Sampling>
3738
- <doc:MinAndMax>
3839
- <doc:Selecting>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Keying and Grouping
2+
3+
Convert a sequence to a dictionary, providing keys to individual elements or to use as grouping values.
4+
5+
## Topics
6+
7+
### Creating a Keyed Dictionary
8+
9+
- ``Swift/Sequence/keyed(by:)``
10+
- ``Swift/Sequence/keyed(by:resolvingConflictsWith:)``
11+
12+
### Grouping Elements by Key
13+
14+
- ``Swift/Sequence/grouped(by:)``

0 commit comments

Comments
 (0)