Skip to content

Commit abe4890

Browse files
[gardening] Adding Guides to compacted()
1 parent 05491db commit abe4890

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Guides/Compacted.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Compacted
2+
3+
[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/Compacted.swift) |
4+
[Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/CompactedTests.swift)]
5+
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.
9+
10+
```swift
11+
let array: [Int?] = [10, nil, 30, nil, 2, 3, nil, 5]
12+
let withNoNils = array.compacted()
13+
// Array(withNoNils) == [10, 30, 2, 3, 5]
14+
15+
```
16+
17+
The most convenient part of `compacted()` is that we avoid the usage of a closure.
18+
19+
## Detailed Design
20+
21+
The `compacted()` methods has two overloads:
22+
23+
```swift
24+
extension Sequence {
25+
public func compacted<Unwrapped>() -> CompactedSequence<Self, Unwrapped> { ... }
26+
}
27+
28+
extension Collection {
29+
public func compacted<Unwrapped>() -> CompactedCollection<Self, Unwrapped> { ... }
30+
}
31+
```
32+
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`,
35+
`LazyCollectionProtocol`, `Equatable` and `Hashable` when base collection conforms to them.
36+
37+
### Naming
38+
39+
The naming method name `compacted()` matches the current method `compactMap` that one of the most common usages `compactMap { $0 }` is abstracted by it.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
4545
- [`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.
4646
- [`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.
4747
- [`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 of collection.
4849

4950
## Adding Swift Algorithms as a Dependency
5051

Sources/Algorithms/Compacted.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ extension CompactedSequence: Equatable
197197

198198
extension CompactedSequence: Hashable
199199
where Element: Hashable {
200+
200201
@inlinable
201202
public func hash(into hasher: inout Hasher) {
202203
for element in self {

0 commit comments

Comments
 (0)