@@ -24,6 +24,26 @@ extension LazySequenceProtocol {
24
24
FlattenSequence < LazyMapSequence < Elements , SegmentOfResult > > > {
25
25
return self . map ( transform) . flatten ( )
26
26
}
27
+
28
+ /// Returns a `LazyMapSequence` containing the concatenated non-nil
29
+ /// results of mapping transform over this `Sequence`. The elements of
30
+ /// the result are computed lazily, each time they are read.
31
+ ///
32
+ /// Use this method to receive only nonoptional values when your
33
+ /// transformation produces an optional value.
34
+ ///
35
+ /// - Parameter transform: A closure that accepts an element of this
36
+ /// sequence as its argument and returns an optional value.
37
+ @warn_unused_result
38
+ public func flatMap< U> (
39
+ _ transform: ( Self . Elements . Iterator . Element ) -> U ?
40
+ ) -> LazyMapSequence <
41
+ LazyFilterSequence <
42
+ LazyMapSequence < Self . Elements , U ? > > ,
43
+ U
44
+ > {
45
+ return self . map ( transform) . filter { $0 != nil } . map { $0! }
46
+ }
27
47
}
28
48
29
49
extension LazyCollectionProtocol {
@@ -42,6 +62,26 @@ extension LazyCollectionProtocol {
42
62
> {
43
63
return self . map ( transform) . flatten ( )
44
64
}
65
+
66
+ /// Returns a `LazyMapCollection` containing the concatenated non-nil
67
+ /// results of mapping transform over this collection. The elements of
68
+ /// the result are computed lazily, each time they are read.
69
+ ///
70
+ /// Use this method to receive only nonoptional values when your
71
+ /// transformation produces an optional value.
72
+ ///
73
+ /// - Parameter transform: A closure that accepts an element of this
74
+ /// collection as its argument and returns an optional value.
75
+ @warn_unused_result
76
+ public func flatMap< U> (
77
+ _ transform: ( Self . Elements . Iterator . Element ) -> U ?
78
+ ) -> LazyMapCollection <
79
+ LazyFilterCollection <
80
+ LazyMapCollection < Self . Elements , U ? > > ,
81
+ U
82
+ > {
83
+ return self . map ( transform) . filter { $0 != nil } . map { $0! }
84
+ }
45
85
}
46
86
47
87
extension LazyCollectionProtocol
@@ -67,4 +107,48 @@ extension LazyCollectionProtocol
67
107
> > {
68
108
return self . map ( transform) . flatten ( )
69
109
}
110
+
111
+ /// Returns a `LazyMapBidirectionalCollection` containing the concatenated non-nil
112
+ /// results of mapping transform over this collection. The elements of
113
+ /// the result are computed lazily, each time they are read.
114
+ ///
115
+ /// Use this method to receive only nonoptional values when your
116
+ /// transformation produces an optional value.
117
+ ///
118
+ /// - Parameter transform: A closure that accepts an element of this
119
+ /// collection as its argument and returns an optional value.
120
+ public func flatMap< U> (
121
+ _ transform: ( Self . Elements . Iterator . Element ) -> U ?
122
+ ) -> LazyMapBidirectionalCollection <
123
+ LazyFilterBidirectionalCollection <
124
+ LazyMapBidirectionalCollection < Self . Elements , U ? > > ,
125
+ U
126
+ > {
127
+ return self . map ( transform) . filter { $0 != nil } . map { $0! }
128
+ }
129
+ }
130
+
131
+ extension LazyCollectionProtocol
132
+ where
133
+ Self : RandomAccessCollection ,
134
+ Elements : RandomAccessCollection
135
+ {
136
+ /// Returns a `LazyMapRandomAccessCollection` containing the concatenated non-nil
137
+ /// results of mapping transform over this collection. The elements of
138
+ /// the result are computed lazily, each time they are read.
139
+ ///
140
+ /// Use this method to receive only nonoptional values when your
141
+ /// transformation produces an optional value.
142
+ ///
143
+ /// - Parameter transform: A closure that accepts an element of this
144
+ /// collection as its argument and returns an optional value.
145
+ public func flatMap< U> (
146
+ _ transform: ( Self . Elements . Iterator . Element ) -> U ?
147
+ ) -> LazyMapRandomAccessCollection <
148
+ LazyFilterRandomAccessCollection <
149
+ LazyMapRandomAccessCollection < Self . Elements , U ? > > ,
150
+ U
151
+ > {
152
+ return self . map ( transform) . filter { $0 != nil } . map { $0! }
153
+ }
70
154
}
0 commit comments