Skip to content

Commit 4e55302

Browse files
committed
Added in flatMap quickly.
1 parent ab93b14 commit 4e55302

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

Sources/ColdSignal.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,9 @@ extension ColdSignalType {
234234
return lift { $0.reduce(initial: initial, combine) }
235235
}
236236

237+
public func flatMap<U>(_ transform: @escaping (Value) -> U?) -> ColdSignal<U, ErrorType> {
238+
return lift { $0.flatMap(transform) }
239+
}
240+
237241
}
238242

Sources/Event.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ public enum Event<Value, ErrorType: Error> {
5858
}
5959
}
6060

61+
/// Lifts the given function over the event's value.
62+
public func flatMap<U>(_ f: (Value) -> U?) -> Event<U, ErrorType>? {
63+
switch self {
64+
case let .Next(value):
65+
if let nextValue = f(value) {
66+
return .Next(nextValue)
67+
}
68+
return nil
69+
70+
case let .Failed(error):
71+
return .Failed(error)
72+
73+
case .Completed:
74+
return .Completed
75+
76+
case .Interrupted:
77+
return .Interrupted
78+
}
79+
}
80+
6181
/// Lifts the given function over the event's error.
6282
public func mapError<F>(_ f: (ErrorType) -> F) -> Event<Value, F> {
6383
switch self {

Sources/Signal.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,14 @@ extension SignalType {
300300
}
301301
}
302302

303+
public func flatMap<U>(_ transform: @escaping (Value) -> U?) -> Signal<U, ErrorType> {
304+
return Signal { observer in
305+
return self.on { event -> Void in
306+
if let e = event.flatMap(transform) {
307+
observer.sendEvent(e)
308+
}
309+
}
310+
}
311+
}
312+
303313
}

0 commit comments

Comments
 (0)