|
| 1 | +# Joined |
| 2 | + |
| 3 | +[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/Joined.swift) | |
| 4 | + [Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/JoinedTests.swift)] |
| 5 | + |
| 6 | +Concatenate a sequence of sequences, inserting a separator between each element. |
| 7 | + |
| 8 | +The separator can be either a single element or a sequence of elements, and it |
| 9 | +can optionally depend on the sequences right before and after it by returning it |
| 10 | +from a closure: |
| 11 | + |
| 12 | +```swift |
| 13 | +for number in [[1], [2, 3], [4, 5, 6]].joined(by: 100) { |
| 14 | + print(number) |
| 15 | +} |
| 16 | +// 1, 100, 2, 3, 100, 4, 5, 6 |
| 17 | + |
| 18 | +for number in [[10], [20, 30], [40, 50, 60]].joined(by: { [$0.count, $1.count] }) { |
| 19 | + print(number) |
| 20 | +} |
| 21 | +// 10, 1, 2, 20, 30, 2, 3, 40, 50, 60 |
| 22 | +``` |
| 23 | + |
| 24 | +## Detailed Design |
| 25 | + |
| 26 | +The versions that take a closure are executed eagerly and are defined on |
| 27 | +`Sequence`: |
| 28 | + |
| 29 | +```swift |
| 30 | +extension Sequence where Element: Sequence { |
| 31 | + public func joined( |
| 32 | + by separator: (Element, Element) throws -> Element.Element |
| 33 | + ) rethrows -> [Element.Element] |
| 34 | + |
| 35 | + public func joined<Separator>( |
| 36 | + by separator: (Element, Element) throws -> Separator |
| 37 | + ) rethrows -> [Element.Element] |
| 38 | + where Separator: Sequence, Separator.Element == Element.Element |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +The versions that do not take a closure are defined on both `Sequence` and |
| 43 | +`Collection` because the resulting collections need to precompute their start |
| 44 | +index to ensure O(1) access: |
| 45 | + |
| 46 | +```swift |
| 47 | +extension Sequence where Element: Sequence { |
| 48 | + public func joined(by separator: Element.Element) |
| 49 | + -> JoinedBySequence<Self, CollectionOfOne<Element.Element>> |
| 50 | + |
| 51 | + public func joined<Separator>( |
| 52 | + by separator: Separator |
| 53 | + ) -> JoinedBySequence<Self, Separator> |
| 54 | + where Separator: Collection, Separator.Element == Element.Element |
| 55 | +} |
| 56 | + |
| 57 | +extension Collection where Element: Sequence { |
| 58 | + public func joined(by separator: Element.Element) |
| 59 | + -> JoinedByCollection<Self, CollectionOfOne<Element.Element>> |
| 60 | + |
| 61 | + public func joined<Separator>( |
| 62 | + by separator: Separator |
| 63 | + ) -> JoinedByCollection<Self, Separator> |
| 64 | + where Separator: Collection, Separator.Element == Element.Element |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Note that the sequence separator of the closure-less version defined on |
| 69 | +`Sequence` is required to be a `Collection`, because a plain `Sequence` cannot in |
| 70 | +general be iterated over multiple times. |
| 71 | + |
| 72 | +The closure-based versions also have lazy variants that are defined on both |
| 73 | +`LazySequenceProtocol` and `LazyCollectionProtocol` for the same reason as |
| 74 | +explained above: |
| 75 | + |
| 76 | +```swift |
| 77 | +extension LazySequenceProtocol where Element: Sequence { |
| 78 | + public func joined( |
| 79 | + by separator: @escaping (Element, Element) -> Element.Element |
| 80 | + ) -> JoinedByClosureSequence<Self, CollectionOfOne<Element.Element>> |
| 81 | + |
| 82 | + public func joined<Separator>( |
| 83 | + by separator: @escaping (Element, Element) -> Separator |
| 84 | + ) -> JoinedByClosureSequence<Self, Separator> |
| 85 | +} |
| 86 | + |
| 87 | +extension LazyCollectionProtocol where Element: Collection { |
| 88 | + public func joined( |
| 89 | + by separator: @escaping (Element, Element) -> Element.Element |
| 90 | + ) -> JoinedByClosureCollection<Self, CollectionOfOne<Element.Element>> |
| 91 | + |
| 92 | + public func joined<Separator>( |
| 93 | + by separator: @escaping (Element, Element) -> Separator |
| 94 | + ) -> JoinedByClosureCollection<Self, Separator> |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +`JoinedBySequence`, `JoinedByClosureSequence`, `JoinedByCollection`, and |
| 99 | +`JoinedByClosureCollection` conform to `LazySequenceProtocol` when the base |
| 100 | +sequence conforms. `JoinedByCollection` and `JoinedByClosureCollection` also |
| 101 | +conform to `LazyCollectionProtocol` and `BidirectionalCollection` when the base |
| 102 | +collection conforms. |
0 commit comments