Skip to content

Commit 31d600a

Browse files
authored
Light revisions to guides (#127)
1 parent 0e44ceb commit 31d600a

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

Guides/Compacted.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,45 @@
33
[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/Compacted.swift) |
44
[Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/CompactedTests.swift)]
55

6-
Convenience method that flatten the `nil`s out of a sequence or collection.
7-
That behaves exactly one of the most common uses of `compactMap` which is `collection.lazy.compactMap { $0 }`
8-
which is only remove `nil`s without transforming the elements.
6+
A convenience method that lazily flattens the `nil`s out of a sequence
7+
or collection.
8+
9+
The new method matches one of the most common uses of `compactMap`,
10+
which is to only remove `nil`s without transforming the elements
11+
(e.g. `collection.lazy.compactMap { $0 }`).
912

1013
```swift
1114
let array: [Int?] = [10, nil, 30, nil, 2, 3, nil, 5]
1215
let withNoNils = array.compacted()
1316
// Array(withNoNils) == [10, 30, 2, 3, 5]
14-
1517
```
1618

17-
The most convenient part of `compacted()` is that we avoid the usage of a closure.
19+
The type returned by `compacted()` lazily computes the non-`nil` elements
20+
without requiring a user to provide a closure or use the `.lazy` property.
1821

1922
## Detailed Design
2023

2124
The `compacted()` methods has two overloads:
2225

2326
```swift
2427
extension Sequence {
25-
public func compacted<Unwrapped>() -> CompactedSequence<Self, Unwrapped> { ... }
28+
public func compacted<Unwrapped>() -> CompactedSequence<Self, Unwrapped>
29+
where Element == Unwrapped?
2630
}
2731

2832
extension Collection {
29-
public func compacted<Unwrapped>() -> CompactedCollection<Self, Unwrapped> { ... }
33+
public func compacted<Unwrapped>() -> CompactedCollection<Self, Unwrapped>
34+
where Element == Unwrapped?
3035
}
3136
```
3237

33-
One is a more general `CompactedSequence` for any `Sequence` base. And the other a more specialized `CompactedCollection`
34-
where base is a `Collection` and with conditional conformance to `BidirectionalCollection`, `RandomAccessCollection` and
35-
`LazyCollectionProtocol` when base collection conforms to them.
38+
The `Sequence` version of `compacted()` returns a `CompactedSequence` type,
39+
while the `Collection` version returns `CompactedCollection`. The collection
40+
has conditional conformance to `BidirectionalCollection` and
41+
`LazyCollectionProtocol` when the base collection conforms.
3642

3743
### Naming
3844

39-
The naming method name `compacted()` matches the current method `compactMap` that one of the most common usages `compactMap { $0 }` is abstracted by it.
45+
The name `compacted()` matches the existing method `compactMap`,
46+
which is commonly used to extract only the non-`nil` values
47+
without mapping them to a different type.

Guides/Cycle.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ for x in (1...3).cycled(times: 3) {
1717
```
1818

1919
`cycled(times:)` provides a more expressive way of repeating a
20-
collection's elements a limited number of times.
20+
collection's elements a limited number of times than
21+
combining `repeatElement(_:count:)` and `joined()`.
2122

2223
## Detailed Design
2324

@@ -35,8 +36,8 @@ The new `Cycle` type is a sequence only, given that the `Collection` protocol
3536
design makes infinitely large types impossible/impractical. `Cycle` also
3637
conforms to `LazySequenceProtocol` when the base type conforms.
3738

38-
Note that the returned `FiniteCycle` will always have `Collection`
39-
conformance, and will have `BidirectionalCollection` conformance
39+
The `FiniteCycle` type always has `Collection` conformance, with
40+
`BidirectionalCollection` conformance
4041
when called on a bidirectional collection. `FiniteCycle` also
4142
conforms to `LazyCollectionProtocol` when the base type conforms.
4243

@@ -61,4 +62,4 @@ anything that matches the `cycled(times:)` behavior.
6162
`cycled(times:)` here.
6263

6364
**Python:** Python’s `itertools` includes a `cycle` method, which caches the
64-
elements of the iterator on the first pass.
65+
elements of the iterator on the first pass.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
2525

2626
#### Subsetting operations
2727

28+
- [`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.
2829
- [`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.
2930
- [`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.
3031
- [`striding(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Stride.md): Returns every nth element of a collection.
@@ -45,7 +46,6 @@ Read more about the package, and the intent behind it, in the [announcement on s
4546
- [`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.
4647
- [`split(maxSplits:omittingEmptySubsequences:whereSeparator)`, `split(separator:maxSplits:omittingEmptySubsequences)`](https://github.com/apple/swift-algorithms/blob/main/Guides/LazySplit.md): Lazy versions of the Standard Library's eager operations that split sequences and collections into subsequences separated by the specified separator element.
4748
- [`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.
48-
- [`compacted()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Compacted.md): Flatten the `nil`s out of a sequence or collection.
4949

5050
## Adding Swift Algorithms as a Dependency
5151

0 commit comments

Comments
 (0)