Skip to content

Commit 039ad2b

Browse files
committed
[stdlib] Rename flatten() to joined() (SE-0133)
1 parent e7e8b66 commit 039ad2b

File tree

8 files changed

+56
-36
lines changed

8 files changed

+56
-36
lines changed

stdlib/public/core/FlatMap.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ extension LazySequenceProtocol {
1414
/// Returns the concatenated results of mapping `transform` over
1515
/// `self`. Equivalent to
1616
///
17-
/// self.map(transform).flatten()
17+
/// self.map(transform).joined()
1818
///
1919
/// - Complexity: O(1)
2020
public func flatMap<SegmentOfResult : Sequence>(
2121
_ transform: (Elements.Iterator.Element) -> SegmentOfResult
2222
) -> LazySequence<
2323
FlattenSequence<LazyMapSequence<Elements, SegmentOfResult>>> {
24-
return self.map(transform).flatten()
24+
return self.map(transform).joined()
2525
}
2626

2727
/// Returns a `LazyMapSequence` containing the concatenated non-nil
@@ -47,7 +47,7 @@ extension LazyCollectionProtocol {
4747
/// Returns the concatenated results of mapping `transform` over
4848
/// `self`. Equivalent to
4949
///
50-
/// self.map(transform).flatten()
50+
/// self.map(transform).joined()
5151
///
5252
/// - Complexity: O(1)
5353
public func flatMap<SegmentOfResult : Collection>(
@@ -56,7 +56,7 @@ extension LazyCollectionProtocol {
5656
FlattenCollection<
5757
LazyMapCollection<Elements, SegmentOfResult>>
5858
> {
59-
return self.map(transform).flatten()
59+
return self.map(transform).joined()
6060
}
6161

6262
/// Returns a `LazyMapCollection` containing the concatenated non-nil
@@ -86,7 +86,7 @@ extension LazyCollectionProtocol
8686
/// Returns the concatenated results of mapping `transform` over
8787
/// `self`. Equivalent to
8888
///
89-
/// self.map(transform).flatten()
89+
/// self.map(transform).joined()
9090
///
9191
/// - Complexity: O(1)
9292
public func flatMap<SegmentOfResult : Collection>(
@@ -95,7 +95,7 @@ extension LazyCollectionProtocol
9595
FlattenBidirectionalCollection<
9696
LazyMapBidirectionalCollection<Elements, SegmentOfResult>>>
9797
where SegmentOfResult : BidirectionalCollection {
98-
return self.map(transform).flatten()
98+
return self.map(transform).joined()
9999
}
100100

101101
/// Returns a `LazyMapBidirectionalCollection` containing the concatenated non-nil

stdlib/public/core/Flatten.swift.gyb

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ public struct FlattenIterator<Base : IteratorProtocol> : IteratorProtocol, Seque
6868
/// The elements of this view are a concatenation of the elements of
6969
/// each sequence in the base.
7070
///
71-
/// The `flatten` property is always lazy, but does not implicitly
71+
/// The `joined` method is always lazy, but does not implicitly
7272
/// confer laziness on algorithms applied to its result. In other
7373
/// words, for ordinary sequences `s`:
7474
///
75-
/// * `s.flatten()` does not create new storage
76-
/// * `s.flatten().map(f)` maps eagerly and returns a new array
77-
/// * `s.lazy.flatten().map(f)` maps lazily and returns a `LazyMapSequence`
75+
/// * `s.joined()` does not create new storage
76+
/// * `s.joined().map(f)` maps eagerly and returns a new array
77+
/// * `s.lazy.joined().map(f)` maps lazily and returns a `LazyMapSequence`
7878
///
7979
/// - See also: `FlattenCollection`
8080
public struct FlattenSequence<Base : Sequence> : Sequence
@@ -113,8 +113,8 @@ extension Sequence where Iterator.Element : Sequence {
113113
/// // Prints "8..<10"
114114
/// // Prints "15..<17"
115115
///
116-
/// // Use 'flatten()' to access each element of each range:
117-
/// for index in ranges.flatten() {
116+
/// // Use 'joined()' to access each element of each range:
117+
/// for index in ranges.joined() {
118118
/// print(index, terminator: " ")
119119
/// }
120120
/// // Prints: "0 1 2 8 9 15 16"
@@ -123,9 +123,14 @@ extension Sequence where Iterator.Element : Sequence {
123123
/// sequence of sequences.
124124
///
125125
/// - SeeAlso: `flatMap(_:)`, `joined(separator:)`
126-
public func flatten() -> FlattenSequence<Self> {
126+
public func joined() -> FlattenSequence<Self> {
127127
return FlattenSequence(_base: self)
128128
}
129+
130+
@available(*, unavailable, renamed: "joined()")
131+
public func flatten() -> FlattenSequence<Self> {
132+
Builtin.unreachable()
133+
}
129134
}
130135

131136
extension LazySequenceProtocol
@@ -134,11 +139,16 @@ extension LazySequenceProtocol
134139
Iterator.Element : Sequence {
135140

136141
/// A concatenation of the elements of `self`.
137-
public func flatten() -> LazySequence<
142+
public func joined() -> LazySequence<
138143
FlattenSequence<Elements>
139144
> {
140145
return FlattenSequence(_base: elements).lazy
141146
}
147+
148+
@available(*, unavailable, renamed: "joined()")
149+
public func flatten() -> LazySequence<FlattenSequence<Elements>> {
150+
Builtin.unreachable()
151+
}
142152
}
143153

144154
% for traversal in ['Forward', 'Bidirectional']:
@@ -209,13 +219,13 @@ extension ${Index} : Comparable {
209219
/// The elements of this view are a concatenation of the elements of
210220
/// each collection in the base.
211221
///
212-
/// The `flatten` property is always lazy, but does not implicitly
222+
/// The `joined` method is always lazy, but does not implicitly
213223
/// confer laziness on algorithms applied to its result. In other
214224
/// words, for ordinary collections `c`:
215225
///
216-
/// * `c.flatten()` does not create new storage
217-
/// * `c.flatten().map(f)` maps eagerly and returns a new array
218-
/// * `c.lazy.flatten().map(f)` maps lazily and returns a `LazyMapCollection`
226+
/// * `c.joined()` does not create new storage
227+
/// * `c.joined().map(f)` maps eagerly and returns a new array
228+
/// * `c.lazy.joined().map(f)` maps lazily and returns a `LazyMapCollection`
219229
///
220230
/// - Note: The performance of accessing `startIndex`, `first`, any methods
221231
/// that depend on `startIndex`, or of advancing a `${Collection}Index`
@@ -381,8 +391,8 @@ extension ${collectionForTraversal(traversal)}
381391
/// // Prints "8..<10"
382392
/// // Prints "15..<17"
383393
///
384-
/// // Use 'flatten()' to access each element of each range:
385-
/// for index in ranges.flatten() {
394+
/// // Use 'joined()' to access each element of each range:
395+
/// for index in ranges.joined() {
386396
/// print(index, terminator: " ")
387397
/// }
388398
/// // Prints: "0 1 2 8 9 15 16"
@@ -391,9 +401,14 @@ extension ${collectionForTraversal(traversal)}
391401
/// collection of collections.
392402
///
393403
/// - SeeAlso: `flatMap(_:)`, `joined(separator:)`
394-
public func flatten() -> ${Collection}<Self> {
404+
public func joined() -> ${Collection}<Self> {
395405
return ${Collection}(self)
396406
}
407+
408+
@available(*, unavailable, renamed: "joined()")
409+
public func flatten() -> ${Collection}<Self> {
410+
Builtin.unreachable()
411+
}
397412
}
398413

399414
extension LazyCollectionProtocol
@@ -404,9 +419,14 @@ extension LazyCollectionProtocol
404419
${constraints % {'Base': 'Elements.'}},
405420
Iterator.Element == Elements.Iterator.Element {
406421
/// A concatenation of the elements of `self`.
407-
public func flatten() -> LazyCollection<${Collection}<Elements>> {
422+
public func joined() -> LazyCollection<${Collection}<Elements>> {
408423
return ${Collection}(elements).lazy
409424
}
425+
426+
@available(*, unavailable, renamed: "joined()")
427+
public func flatten() -> LazyCollection<${Collection}<Elements>> {
428+
Builtin.unreachable()
429+
}
410430
}
411431

412432
% end

stdlib/public/core/Sequence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ extension Sequence {
813813

814814
if i != ringBuffer.startIndex {
815815
return AnySequence(
816-
[ringBuffer[i..<ringBuffer.endIndex], ringBuffer[0..<i]].flatten())
816+
[ringBuffer[i..<ringBuffer.endIndex], ringBuffer[0..<i]].joined())
817817
}
818818
return AnySequence(ringBuffer)
819819
}

stdlib/public/core/SequenceAlgorithms.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ extension Sequence {
615615
/// // [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
616616
///
617617
/// In fact, `s.flatMap(transform)` is equivalent to
618-
/// `Array(s.map(transform).flatten())`.
618+
/// `Array(s.map(transform).joined())`.
619619
///
620620
/// - Parameter transform: A closure that accepts an element of this
621621
/// sequence as its argument and returns a sequence or collection.

test/Prototypes/Algorithms.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ suite.test("reverseSubrange") {
691691
b.reverseSubrange(p..<q)
692692
expectEqual(
693693
b,
694-
Array([prefix, ArraySlice(a[p..<q].reversed()), suffix].flatten()))
694+
Array([prefix, ArraySlice(a[p..<q].reversed()), suffix].joined()))
695695
}
696696
}
697697
}
@@ -709,7 +709,7 @@ suite.test("rotate") {
709709
for m in p...q {
710710
var b = a
711711
let r0 = b._rotateSubrangeForward(p..<q, shiftingToStart: m)
712-
let rotated = Array([prefix, a[m..<q], a[p..<m], suffix].flatten())
712+
let rotated = Array([prefix, a[m..<q], a[p..<m], suffix].joined())
713713
expectEqual(b, rotated)
714714
expectEqual(r0, a.index(p, offsetBy: a[m..<q].count))
715715

@@ -738,7 +738,7 @@ suite.test("rotateRandomAccess") {
738738
for m in p...q {
739739
var b = a
740740
let r0 = b[p..<q].rotateRandomAccess(shiftingToStart: m)
741-
let rotated = Array([prefix, a[m..<q], a[p..<m], suffix].flatten())
741+
let rotated = Array([prefix, a[m..<q], a[p..<m], suffix].joined())
742742
expectEqual(b, rotated)
743743
expectEqual(r0, a.index(p, offsetBy: a[m..<q].count))
744744

test/Prototypes/CollectionTransformers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public struct ArrayBuilder<T> : CollectionBuilder {
173173
_resultParts.append(_resultTail)
174174
_resultTail = []
175175
// FIXME: optimize. parallelize.
176-
return Array(_resultParts.flatten())
176+
return Array(_resultParts.joined())
177177
}
178178
}
179179

validation-test/stdlib/Concatenate.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ let samples: ContiguousArray<(CountableRange<Int>, X)> = [
3333

3434
for (expected, source) in samples {
3535
ConcatenateTests.test("forward-\(source)") {
36-
checkBidirectionalCollection(expected, source.flatten())
36+
checkBidirectionalCollection(expected, source.joined())
3737
}
3838

3939
ConcatenateTests.test("reverse-\(source)") {
4040
// FIXME: separate 'expected' and 'reversed' variables are a workaround
4141
// for: <rdar://problem/20789500>
4242
let expected = ContiguousArray(expected.lazy.reversed())
43-
let reversed = source.flatten().reversed()
43+
let reversed = source.joined().reversed()
4444
checkBidirectionalCollection(expected, reversed)
4545
}
4646

4747
ConcatenateTests.test("sequence-\(source)") {
4848
checkSequence(
4949
ContiguousArray(expected),
50-
AnySequence(source).flatten())
50+
AnySequence(source).joined())
5151
}
5252
}
5353

validation-test/stdlib/Lazy.swift.gyb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,14 +1219,14 @@ do {
12191219
tests.test("FlattenSequence/\(data)") {
12201220
var base = MinimalSequence(
12211221
elements: data.map { MinimalSequence(elements: $0) })
1222-
checkSequence(expected, base.flatten(), resiliencyChecks: .none)
1222+
checkSequence(expected, base.joined(), resiliencyChecks: .none)
12231223

12241224
// Checking that flatten doesn't introduce laziness
12251225

12261226
// checkSequence consumed base, so reassign
12271227
base = MinimalSequence(
12281228
elements: data.map { MinimalSequence(elements: $0) })
1229-
let flattened = base.flatten()
1229+
let flattened = base.joined()
12301230
var calls = 0
12311231
_ = flattened.map { _ in calls += 1 }
12321232
expectEqual(
@@ -1240,7 +1240,7 @@ do {
12401240
elements: data.map { MinimalSequence(elements: $0) }
12411241
).lazy.map { $0 }
12421242

1243-
let flattened = base.flatten()
1243+
let flattened = base.joined()
12441244
var calls = 0
12451245
_ = flattened.map { _ in calls += 1 }
12461246
expectEqual(0, calls, "unexpected eagerness in \(flattened.dynamicType)")
@@ -1252,7 +1252,7 @@ do {
12521252
let base = Minimal${TraversalCollection}(
12531253
elements: data.map { Minimal${TraversalCollection}(elements: $0) })
12541254

1255-
let flattened = base.flatten()
1255+
let flattened = base.joined()
12561256
check${Traversal}Collection(expected, flattened, resiliencyChecks: .none)
12571257

12581258
// Checking that flatten doesn't introduce laziness
@@ -1269,7 +1269,7 @@ do {
12691269
elements: data.map { Minimal${TraversalCollection}(elements: $0) }
12701270
).lazy.map { $0 }
12711271

1272-
let flattened = base.flatten()
1272+
let flattened = base.joined()
12731273

12741274
var calls = 0
12751275
_ = flattened.map { _ in calls += 1 }

0 commit comments

Comments
 (0)