Skip to content

Commit b50ed7e

Browse files
[Docs] Adding Guides to compacted()
1 parent 9d7d2f4 commit b50ed7e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-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
@@ -42,6 +42,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
4242
- [`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.
4343
- [`striding(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Stride.md): Returns every nth element of a collection.
4444
- [`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.
45+
- [`compacted()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Compacted.md): Flatten the `nil`s out of a sequence of collection.
4546

4647
## Adding Swift Algorithms as a Dependency
4748

0 commit comments

Comments
 (0)