Skip to content

Commit 4f53c9c

Browse files
authored
Finish sub-page documentation updates (#204)
* Finish sub-page documentation updates Some of these pages could use introductions, but the current state is ready for publication. * Add SPI yaml file for building docs
1 parent 0d6f943 commit 4f53c9c

File tree

10 files changed

+129
-16
lines changed

10 files changed

+129
-16
lines changed

.spi.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: 1
2+
builder:
3+
configs:
4+
- documentation_targets: [Algorithms]
5+

Sources/Algorithms/Chunked.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,8 @@ extension ChunksOfCountCollection {
777777
}
778778

779779
extension Collection {
780-
/// Returns a collection of subsequences of this collection, each with the
781-
/// specified length.
780+
/// Returns a collection of subsequences, each with up to the specified
781+
/// length.
782782
///
783783
/// If the number of elements in the collection is evenly divided by `count`,
784784
/// then every chunk will have a length equal to `count`. Otherwise, every
@@ -792,8 +792,7 @@ extension Collection {
792792
/// // [1, 2, 3, 4, 5]
793793
/// // [6, 7, 8, 9, 10]
794794
///
795-
/// print(c.chunks(ofCount: 3).map(Array.init))
796-
/// for chunk in numbers.chunks(ofCount: 5) {
795+
/// for chunk in numbers.chunks(ofCount: 3) {
797796
/// print(chunk)
798797
/// }
799798
/// // [1, 2, 3]
@@ -808,6 +807,7 @@ extension Collection {
808807
///
809808
/// - Complexity: O(1) if the collection conforms to `RandomAccessCollection`;
810809
/// otherwise, O(*k*), where *k* is equal to `count`.
810+
///
811811
@inlinable
812812
public func chunks(ofCount count: Int) -> ChunksOfCountCollection<Self> {
813813
precondition(count > 0, "Cannot chunk with count <= 0!")
@@ -828,7 +828,8 @@ extension ChunksOfCountCollection: LazyCollectionProtocol
828828
//===----------------------------------------------------------------------===//
829829

830830
extension Collection {
831-
/// Returns a collection of evenly divided subsequences of this collection.
831+
/// Returns a collection of evenly divided consecutive subsequences of this
832+
/// collection.
832833
///
833834
/// This method divides the collection into a given number of evenly sized
834835
/// chunks. If the length of the collection is not divisible by `count`, the

Sources/Algorithms/Documentation.docc/Algorithms.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,25 @@ along with their related types.
55

66
## Overview
77

8-
This library adds a variety of extended operations to the Swift standard library's
9-
`Sequence` and `Collection` protocols, via extension methods and global functions.
8+
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.
9+
10+
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:
11+
12+
```swift
13+
let numbers = [10, 20, 30, 10, 40, 40, 10, 20]
14+
let chunks = numbers.chunked(by: { $0 <= $1 })
15+
// [[10, 20, 30], [10, 40, 40], [10, 20]]
16+
```
17+
18+
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:
19+
20+
```swift
21+
let names = ["Cassie", "Chloe", "Jasmine", "Jordan", "Taylor"]
22+
let chunks = names.chunked(on: \.first)
23+
// [["Cassie", "Chloe"], ["Jasmine", "Jordan"], ["Taylor"]]
24+
```
25+
26+
Explore more chunking methods and the remainder of the Algorithms package, grouped in the following topics.
1027

1128
## Topics
1229

Sources/Algorithms/Documentation.docc/Chunking.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ Break collections into consecutive chunks by length, count, or based on closure-
44

55
## Overview
66

7+
_Chunking_ is the process of breaking a collection into consecutive subsequences, without dropping or duplicating any of the collection's elements. After chunking a collection, joining the resulting subsequences produces the original collection of elements, unlike _splitting_, which consumes the separator element(s).
8+
9+
```swift
10+
let names = ["Ji-sun", "Jin-su", "Min-jae", "Young-ho"]
11+
let evenlyChunked = names.chunks(ofCount: 2)
12+
// ~ [["Ji-sun", "Jin-su"], ["Min-jae", "Young-ho"]]
13+
14+
let chunkedByFirstLetter = names.chunked(on: \.first)
15+
// equivalent to [("J", ["Ji-sun", "Jin-su"]), ("M", ["Min-jae"]), ("Y", ["Young-ho"])]
16+
```
17+
718
## Topics
819

920
### Chunking a Collection by Count
@@ -13,10 +24,12 @@ Break collections into consecutive chunks by length, count, or based on closure-
1324

1425
### Chunking a Collection by Predicate
1526

16-
- ``Swift/Collection/chunked(on:)``
1727
- ``Swift/Collection/chunked(by:)``
1828
- ``Swift/LazySequenceProtocol/chunked(by:)``
19-
- ``Swift/LazySequenceProtocol/chunked(by:)``
29+
30+
### Chunking a Collection by Subject
31+
32+
- ``Swift/Collection/chunked(on:)``
2033
- ``Swift/LazySequenceProtocol/chunked(on:)``
2134

2235
### Supporting Types

Sources/Algorithms/Documentation.docc/DeprecatedScan.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# DeprecatedScan
22

3-
These methods are deprecated, use the `reductions` family of methods instead.
3+
These methods are deprecated; use the `reductions` family of methods instead.
44

5-
## Overview
5+
See: <doc:Reductions>
66

77
## Topics
88

Sources/Algorithms/Documentation.docc/Extending.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@
33
Chain two collections end-to-end,
44
or repeat a collection forever or a specific number of times.
55

6+
## Overview
7+
8+
_Chaining_ two collections
9+
10+
```swift
11+
let letters = chain("abcd", "EFGH")
12+
// String(letters) == "abcdEFGH"
13+
14+
for (num, letter) in zip((1...3).cycled(), letters) {
15+
print(num, letter)
16+
}
17+
// 1 a
18+
// 2 b
19+
// 3 c
20+
// 1 d
21+
// 2 E
22+
// 3 F
23+
// 1 G
24+
// 2 H
25+
```
26+
627
## Topics
728

829
### Chaining Two Collections

Sources/Algorithms/Documentation.docc/Filtering.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,32 @@ Remove duplicated elements or strip the `nil` values from a sequence or collecti
44

55
## Overview
66

7-
<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@-->
7+
Use the _uniquing_ methods to remove duplicates from a sequence or collection, or to remove elements that have a duplicated property.
8+
9+
```swift
10+
let numbers = [1, 2, 3, 3, 2, 3, 3, 2, 2, 2, 1]
11+
12+
let unique = numbers.uniqued()
13+
// Array(unique) == [1, 2, 3]
14+
```
15+
16+
The `compacted()` method removes all `nil` values from a sequence or collection of optionals:
17+
18+
```swift
19+
let array: [Int?] = [10, nil, 30, nil, 2, 3, nil, 5]
20+
let withNoNils = array.compacted()
21+
// Array(withNoNils) == [10, 30, 2, 3, 5]
22+
```
823

924
## Topics
1025

11-
### Uniqueing Elements
26+
### Uniquing Elements
1227

1328
- ``Swift/Sequence/uniqued()``
1429
- ``Swift/Sequence/uniqued(on:)``
1530
- ``Swift/LazySequenceProtocol/uniqued(on:)``
1631

17-
### Filtering out nil Elements
32+
### Filtering out `nil` Elements
1833

1934
- ``Swift/Collection/compacted()``
2035
- ``Swift/Sequence/compacted()``

Sources/Algorithms/Documentation.docc/Partitioning.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@ Partition a collection according to a unary predicate,
44
rotate a collection around a particular index,
55
or find the index where a collection is already partitioned.
66

7+
## Overview
8+
9+
A _stable partition_ maintains the relative order of elements within both partitions.
10+
11+
```swift
12+
// partition(by:) - unstable ordering
13+
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
14+
let p1 = numbers.partition(by: { $0.isMultiple(of: 20) })
15+
// p1 == 4
16+
// numbers == [10, 70, 30, 50, 40, 60, 20, 80]
17+
// ^ start of second partition
18+
19+
// stablePartition(by:) - maintains relative ordering
20+
numbers = [10, 20, 30, 40, 50, 60, 70, 80]
21+
let p2 = numbers.stablePartition(by: { $0.isMultiple(of: 20) })
22+
// p2 == 4
23+
// numbers == [10, 30, 50, 70, 20, 40, 60, 80]
24+
// ^ start of second partition
25+
```
26+
27+
Use the `rotate` method to shift the elements of a collection to start at a new position, moving the displaced elements to the end:
28+
29+
```swift
30+
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
31+
let p = numbers.rotate(toStartAt: 2)
32+
// numbers == [30, 40, 50, 60, 70, 80, 10, 20]
33+
// p == 6 -- numbers[p] == 10
34+
```
35+
736
## Topics
837

938
### Stable Partition

Sources/Algorithms/Documentation.docc/Reductions.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
Find the incremental values of a sequence "reduce" operation.
44

5+
## Overview
6+
7+
Call one of the `reductions` methods when you want the result of a `reduce` operation along with all of its intermediate values:
8+
9+
```swift
10+
let exclusiveRunningTotal = (1...5).reductions(0, +)
11+
print(exclusiveRunningTotal)
12+
// prints [0, 1, 3, 6, 10, 15]
13+
14+
let inclusiveRunningTotal = (1...5).reductions(+)
15+
print(inclusiveRunningTotal)
16+
// prints [1, 3, 6, 10, 15]
17+
```
18+
519
## Topics
620

721
- ``Swift/Sequence/reductions(_:)``

Sources/Algorithms/Documentation.docc/SlicingSplitting.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ Iterate over tuple pairs of adjacent elements, overlapping windows of a specifie
1515

1616
### Lazily Splitting a Collection
1717

18-
These methods…
19-
2018
- ``Swift/LazySequenceProtocol/split(separator:maxSplits:omittingEmptySubsequences:)-4q4x8``
2119
- ``Swift/LazySequenceProtocol/split(maxSplits:omittingEmptySubsequences:whereSeparator:)-68oqf``
2220
- ``Swift/LazySequenceProtocol/split(separator:maxSplits:omittingEmptySubsequences:)-a46s``

0 commit comments

Comments
 (0)