|
3 | 3 | [[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/Compacted.swift) |
|
4 | 4 | [Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/CompactedTests.swift)]
|
5 | 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. |
| 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 }`). |
9 | 12 |
|
10 | 13 | ```swift
|
11 | 14 | let array: [Int?] = [10, nil, 30, nil, 2, 3, nil, 5]
|
12 | 15 | let withNoNils = array.compacted()
|
13 | 16 | // Array(withNoNils) == [10, 30, 2, 3, 5]
|
14 |
| - |
15 | 17 | ```
|
16 | 18 |
|
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. |
18 | 21 |
|
19 | 22 | ## Detailed Design
|
20 | 23 |
|
21 | 24 | The `compacted()` methods has two overloads:
|
22 | 25 |
|
23 | 26 | ```swift
|
24 | 27 | extension Sequence {
|
25 |
| - public func compacted<Unwrapped>() -> CompactedSequence<Self, Unwrapped> { ... } |
| 28 | + public func compacted<Unwrapped>() -> CompactedSequence<Self, Unwrapped> |
| 29 | + where Element == Unwrapped? |
26 | 30 | }
|
27 | 31 |
|
28 | 32 | extension Collection {
|
29 |
| - public func compacted<Unwrapped>() -> CompactedCollection<Self, Unwrapped> { ... } |
| 33 | + public func compacted<Unwrapped>() -> CompactedCollection<Self, Unwrapped> |
| 34 | + where Element == Unwrapped? |
30 | 35 | }
|
31 | 36 | ```
|
32 | 37 |
|
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. |
36 | 42 |
|
37 | 43 | ### Naming
|
38 | 44 |
|
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. |
0 commit comments