@@ -12,11 +12,26 @@ extension EventLoopFuture where Value: Sequence {
12
12
/// - parameters:
13
13
/// - transform: The closure that each element in the sequence is passed into.
14
14
/// - element: The element from the sequence that you can operate on.
15
- /// - returns: A new `EventLoopFuture` that wraps that sequence of transformed elements.
16
- public func mapEach< Result> ( _ transform: @escaping ( _ element: Value . Element ) -> Result ) -> EventLoopFuture < [ Result ] > {
17
- return self . map { sequence -> [ Result ] in
18
- return sequence. map ( transform)
19
- }
15
+ /// - returns: A new `EventLoopFuture` that wraps the sequence of transformed elements.
16
+ public func mapEach< Result> (
17
+ _ transform: @escaping ( _ element: Value . Element ) -> Result
18
+ ) -> EventLoopFuture < [ Result ] > {
19
+ return self . map { $0. map ( transform) }
20
+ }
21
+
22
+ /// Gets the value of a key path for each element in the sequence that is wrapped by an `EventLoopFuture`.
23
+ ///
24
+ /// let collection = eventLoop.future(["a", "bb", "ccc", "dddd", "eeeee"])
25
+ /// let lengths = collection.mapEach(\.count)
26
+ /// // lengths: EventLoopFuture([1, 2, 3, 4, 5])
27
+ ///
28
+ /// - parameters:
29
+ /// - keyPath: The key path to access on each element in the sequence.
30
+ /// - returns: A new `EventLoopFuture` that wraps the sequence of key path values.
31
+ public func mapEach< Result> (
32
+ _ keyPath: KeyPath < Value . Element , Result >
33
+ ) -> EventLoopFuture < [ Result ] > {
34
+ return self . map { $0. map { $0 [ keyPath: keyPath] } }
20
35
}
21
36
22
37
/// Calls a closure, which returns an `Optional`, on each element in the sequence that is wrapped by an `EventLoopFuture`.
@@ -30,13 +45,60 @@ extension EventLoopFuture where Value: Sequence {
30
45
/// - parameters:
31
46
/// - transform: The closure that each element in the sequence is passed into.
32
47
/// - element: The element from the sequence that you can operate on.
33
- /// - returns: A new `EventLoopFuture` that wraps that sequence of transformed elements.
48
+ /// - returns: A new `EventLoopFuture` that wraps the sequence of transformed elements.
34
49
public func mapEachCompact< Result> (
35
50
_ transform: @escaping ( _ element: Value . Element ) -> Result ?
36
51
) -> EventLoopFuture < [ Result ] > {
37
- return self . map { sequence -> [ Result ] in
38
- return sequence. compactMap ( transform)
39
- }
52
+ return self . map { $0. compactMap ( transform) }
53
+ }
54
+
55
+ /// Gets the optional value of a key path for each element in the sequence that is wrapped by an `EventLoopFuture`.
56
+ ///
57
+ /// let collection = eventLoop.future(["asdf", "qwer", "zxcv", ""])
58
+ /// let letters = collection.mapEachCompact(\.first)
59
+ /// // letters: EventLoopFuture(["a", "q", "z"])
60
+ ///
61
+ /// - parameters:
62
+ /// - keyPath: The key path to access on each element in the sequence.
63
+ /// - returns: A new `EventLoopFuture` that wraps the sequence of non-nil key path values.
64
+ public func mapEachCompact< Result> (
65
+ _ keyPath: KeyPath < Value . Element , Result ? >
66
+ ) -> EventLoopFuture < [ Result ] > {
67
+ return self . map { $0. compactMap { $0 [ keyPath: keyPath] } }
68
+ }
69
+
70
+ /// Calls a closure which returns a collection on each element in the sequence that is wrapped by an `EventLoopFuture`,
71
+ /// combining the results into a single result collection.
72
+ ///
73
+ /// let collection = eventLoop.future([[1, 2, 3], [9, 8, 7], [], [0]])
74
+ /// let flat = collection.mapEachFlat { $0 }
75
+ /// // flat: [1, 2, 3, 9, 8, 7, 0]
76
+ ///
77
+ /// - parameters:
78
+ /// - transform: The closure that each element in the sequence is passed into.
79
+ /// - element: The element from the sequence that you can operate on.
80
+ /// - returns: A new `EventLoopFuture` that wraps the flattened sequence of transformed elements.
81
+ public func mapEachFlat< ResultSegment: Sequence > (
82
+ _ transform: @escaping ( _ element: Value . Element ) -> ResultSegment
83
+ ) -> EventLoopFuture < [ ResultSegment . Element ] > {
84
+ return self . map { $0. flatMap ( transform) }
85
+ }
86
+
87
+ /// Gets the collection value of a key path for each element in the sequence that is wrapped by an `EventLoopFuture`,
88
+ /// combining the results into a single result collection.
89
+ ///
90
+ /// let collection = eventLoop.future(["ABC", "👩👩👧👧"])
91
+ /// let flat = collection.mapEachFlat(\.utf8CString)
92
+ /// // flat: [65, 66, 67, 0, -16, -97, -111, -87, -30, -128, -115, -16, -97, -111, -87, -30,
93
+ /// // -128, -115, -16, -97, -111, -89, -30, -128, -115, -16, -97, -111, -89, 0]
94
+ ///
95
+ /// - parameters:
96
+ /// - keyPath: The key path to access on each element in the sequence.
97
+ /// - returns: A new `EventLoopFuture` that wraps the flattened sequence of transformed elements.
98
+ public func mapEachFlat< ResultSegment: Sequence > (
99
+ _ keyPath: KeyPath < Value . Element , ResultSegment >
100
+ ) -> EventLoopFuture < [ ResultSegment . Element ] > {
101
+ return self . map { $0. flatMap { $0 [ keyPath: keyPath] } }
40
102
}
41
103
42
104
/// Calls a closure, which returns an `EventLoopFuture`, on each element
@@ -111,8 +173,10 @@ extension EventLoopFuture where Value: Sequence {
111
173
/// - parameters:
112
174
/// - transform: The closure that each element in the sequence is passed into.
113
175
/// - element: The element from the sequence that you can operate on.
114
- /// - returns: A new `EventLoopFuture` that wraps that sequence of transformed elements.
115
- public func flatMapEachThrowing< Result> ( _ transform: @escaping ( _ element: Value . Element ) throws -> Result ) -> EventLoopFuture < [ Result ] > {
176
+ /// - returns: A new `EventLoopFuture` that wraps the sequence of transformed elements.
177
+ public func flatMapEachThrowing< Result> (
178
+ _ transform: @escaping ( _ element: Value . Element ) throws -> Result
179
+ ) -> EventLoopFuture < [ Result ] > {
116
180
return self . flatMapThrowing { sequence -> [ Result ] in
117
181
return try sequence. map ( transform)
118
182
}
@@ -131,7 +195,7 @@ extension EventLoopFuture where Value: Sequence {
131
195
/// - parameters:
132
196
/// - transform: The closure that each element in the sequence is passed into.
133
197
/// - element: The element from the sequence that you can operate on.
134
- /// - returns: A new `EventLoopFuture` that wraps that sequence of transformed elements.
198
+ /// - returns: A new `EventLoopFuture` that wraps the sequence of transformed elements.
135
199
public func flatMapEachCompactThrowing< Result> (
136
200
_ transform: @escaping ( _ element: Value . Element ) throws -> Result ?
137
201
) -> EventLoopFuture < [ Result ] > {
0 commit comments