1
- //===--- Map.swift.gyb - Lazily map over a Sequence -----------*- swift -*-===//
1
+ //===--- Map.swift - Lazily map over a Sequence ---- -----------*- swift -*-===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- % {
14
- from gyb_stdlib_support import (
15
- TRAVERSALS,
16
- collectionForTraversal
17
- )
18
- } %
19
-
20
13
/// The `IteratorProtocol` used by `MapSequence` and `MapCollection`.
21
14
/// Produces each element by passing the output of the `Base`
22
15
/// `IteratorProtocol` through a transform function returning `Element`.
@@ -52,8 +45,7 @@ public struct LazyMapIterator<
52
45
}
53
46
}
54
47
55
- /// A `Sequence` whose elements consist of those in a `Base`
56
- /// `Sequence` passed through a transform function returning `Element`.
48
+ /// A `Sequence` whose elements consist of those in a `Base` /// `Sequence` passed through a transform function returning `Element`.
57
49
/// These elements are computed lazily, each time they're read, by
58
50
/// calling the transform function on a base element.
59
51
@_fixed_layout
@@ -96,22 +88,14 @@ public struct LazyMapSequence<Base : Sequence, Element>
96
88
97
89
//===--- Collections ------------------------------------------------------===//
98
90
99
- // FIXME(ABI)#45 (Conditional Conformance): `LazyMap*Collection` types should be
100
- // collapsed into one `LazyMapCollection` using conditional conformances.
101
- // Maybe even combined with `LazyMapSequence`.
102
- // rdar://problem/17144340
103
-
104
- % for Traversal in TRAVERSALS:
105
- % Self = " LazyMap " + collectionForTraversal( Traversal)
106
-
107
91
/// A `Collection` whose elements consist of those in a `Base`
108
92
/// `Collection` passed through a transform function returning `Element`.
109
93
/// These elements are computed lazily, each time they're read, by
110
94
/// calling the transform function on a base element.
111
95
@_fixed_layout
112
- public struct $ { Self } <
113
- Base : $ { collectionForTraversal ( Traversal ) } , Element
114
- > : LazyCollectionProtocol, $ { collectionForTraversal ( Traversal ) } {
96
+ public struct LazyMapCollection <
97
+ Base : Collection , Element
98
+ > : LazyCollectionProtocol , Collection {
115
99
116
100
// FIXME(compiler limitation): should be inferable.
117
101
public typealias Index = Base . Index
@@ -129,16 +113,6 @@ public struct ${Self}<
129
113
_base. formIndex ( after: & i)
130
114
}
131
115
132
- % if Traversal in [ 'Bidirectional', 'RandomAccess'] :
133
- @_inlineable
134
- public func index( before i: Index ) -> Index { return _base. index ( before: i) }
135
-
136
- @_inlineable
137
- public func formIndex( before i: inout Index ) {
138
- _base. formIndex ( before: & i)
139
- }
140
- % end
141
-
142
116
/// Accesses the element at `position`.
143
117
///
144
118
/// - Precondition: `position` is a valid position in `self` and
@@ -148,7 +122,7 @@ public struct ${Self}<
148
122
return _transform ( _base [ position] )
149
123
}
150
124
151
- public typealias SubSequence = $ { Self } < Base . SubSequence, Element>
125
+ public typealias SubSequence = LazyMapCollection < Base . SubSequence , Element >
152
126
153
127
@_inlineable
154
128
public subscript( bounds: Range < Base . Index > ) -> SubSequence {
@@ -183,11 +157,6 @@ public struct ${Self}<
183
157
@_inlineable
184
158
public var first : Element ? { return _base. first. map ( _transform) }
185
159
186
- % if Traversal in [ 'Bidirectional', 'RandomAccess'] :
187
- @_inlineable
188
- public var last : Element ? { return _base. last. map ( _transform) }
189
- % end
190
-
191
160
@_inlineable
192
161
public func index( _ i: Index , offsetBy n: Int ) -> Index {
193
162
return _base. index ( i, offsetBy: n)
@@ -233,7 +202,24 @@ public struct ${Self}<
233
202
internal let _transform : ( Base . Element ) -> Element
234
203
}
235
204
236
- % end
205
+ extension LazyMapCollection : BidirectionalCollection
206
+ where Base : BidirectionalCollection {
207
+
208
+ @_inlineable
209
+ public func index( before i: Index ) -> Index { return _base. index ( before: i) }
210
+
211
+ @_inlineable
212
+ public func formIndex( before i: inout Index ) {
213
+ _base. formIndex ( before: & i)
214
+ }
215
+
216
+ @_inlineable
217
+ public var last : Element ? { return _base. last. map ( _transform) }
218
+ }
219
+
220
+ extension LazyMapCollection : RandomAccessCollection
221
+ where Base : RandomAccessCollection { }
222
+
237
223
238
224
//===--- Support for s.lazy -----------------------------------------------===//
239
225
@@ -249,30 +235,18 @@ extension LazySequenceProtocol {
249
235
}
250
236
}
251
237
252
- % for Traversal in TRAVERSALS:
253
-
254
- extension LazyCollectionProtocol
255
- % if Traversal != 'Forward':
256
- where
257
- Self : ${ collectionForTraversal ( Traversal) } ,
258
- Elements : ${ collectionForTraversal ( Traversal) }
259
- % end
260
- {
238
+ extension LazyCollectionProtocol {
261
239
/// Returns a `LazyMapCollection` over this `Collection`. The elements of
262
240
/// the result are computed lazily, each time they are read, by
263
241
/// calling `transform` function on a base element.
264
242
@_inlineable
265
243
public func map< U> (
266
244
_ transform: @escaping ( Elements . Element ) -> U
267
- ) -> LazyMap ${ collectionForTraversal( Traversal) } < Self . Elements , U > {
268
- return LazyMap ${ collectionForTraversal ( Traversal) } (
269
- _base: self . elements,
270
- transform: transform)
245
+ ) -> LazyMapCollection < Self . Elements , U > {
246
+ return LazyMapCollection ( _base: self . elements, transform: transform)
271
247
}
272
248
}
273
249
274
- % end
275
-
276
250
extension LazyMapCollection {
277
251
// This overload is needed to re-enable Swift 3 source compatibility related
278
252
// to a bugfix in ranking behavior of the constraint solver.
@@ -289,6 +263,7 @@ extension LazyMapCollection {
289
263
}
290
264
}
291
265
292
- // ${'Local Variables'}:
293
- // eval: (read-only-mode 1)
294
- // End:
266
+ @available ( * , deprecated, renamed: " LazyMapCollection " )
267
+ public typealias LazyMapBidirectionalCollection < T, E> = LazyMapCollection < T , E > where T : BidirectionalCollection
268
+ @available ( * , deprecated, renamed: " LazyMapCollection " )
269
+ public typealias LazyMapRandomAccessCollection < T, E> = LazyMapCollection < T , E > where T : Rando mAccessCollection
0 commit comments