@@ -24,6 +24,25 @@ 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`.
30
+ ///
31
+ /// Use this method to receive only nonoptional values when your
32
+ /// transformation produces an optional value.
33
+ ///
34
+ /// - Parameter transform: A closure that accepts an element of this
35
+ /// sequence as its argument and returns an optional value.
36
+ @warn_unused_result
37
+ public func flatMap< ElementOfResult> (
38
+ _ transform: ( Elements . Iterator . Element ) -> ElementOfResult ?
39
+ ) -> LazyMapSequence <
40
+ LazyFilterSequence <
41
+ LazyMapSequence < Elements , ElementOfResult ? > > ,
42
+ ElementOfResult
43
+ > {
44
+ return self . map ( transform) . filter { $0 != nil } . map { $0! }
45
+ }
27
46
}
28
47
29
48
extension LazyCollectionProtocol {
@@ -42,6 +61,25 @@ extension LazyCollectionProtocol {
42
61
> {
43
62
return self . map ( transform) . flatten ( )
44
63
}
64
+
65
+ /// Returns a `LazyMapCollection` containing the concatenated non-nil
66
+ /// results of mapping transform over this collection.
67
+ ///
68
+ /// Use this method to receive only nonoptional values when your
69
+ /// transformation produces an optional value.
70
+ ///
71
+ /// - Parameter transform: A closure that accepts an element of this
72
+ /// collection as its argument and returns an optional value.
73
+ @warn_unused_result
74
+ public func flatMap< ElementOfResult> (
75
+ _ transform: ( Elements . Iterator . Element ) -> ElementOfResult ?
76
+ ) -> LazyMapCollection <
77
+ LazyFilterCollection <
78
+ LazyMapCollection < Elements , ElementOfResult ? > > ,
79
+ ElementOfResult
80
+ > {
81
+ return self . map ( transform) . filter { $0 != nil } . map { $0! }
82
+ }
45
83
}
46
84
47
85
extension LazyCollectionProtocol
@@ -67,4 +105,22 @@ extension LazyCollectionProtocol
67
105
> > {
68
106
return self . map ( transform) . flatten ( )
69
107
}
108
+
109
+ /// Returns a `LazyMapBidirectionalCollection` containing the concatenated non-nil
110
+ /// results of mapping transform over this collection.
111
+ ///
112
+ /// Use this method to receive only nonoptional values when your
113
+ /// transformation produces an optional value.
114
+ ///
115
+ /// - Parameter transform: A closure that accepts an element of this
116
+ /// collection as its argument and returns an optional value.
117
+ public func flatMap< ElementOfResult> (
118
+ _ transform: ( Elements . Iterator . Element ) -> ElementOfResult ?
119
+ ) -> LazyMapBidirectionalCollection <
120
+ LazyFilterBidirectionalCollection <
121
+ LazyMapBidirectionalCollection < Elements , ElementOfResult ? > > ,
122
+ ElementOfResult
123
+ > {
124
+ return self . map ( transform) . filter { $0 != nil } . map { $0! }
125
+ }
70
126
}
0 commit comments