Skip to content

Commit 16d8000

Browse files
committed
Added partition for signal and cold signal
1 parent 706f670 commit 16d8000

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

Sources/ColdSignal.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,22 @@ public extension ColdSignalType {
220220
}
221221
}
222222

223+
public func lift<U, F>(_ transform: @escaping (Signal<Value, ErrorType>) -> (Signal<U, F>, Signal<U, F>))
224+
-> (ColdSignal<U, F>, ColdSignal<U, F>)
225+
{
226+
let (pipeSignal, pipeObserver) = Signal<Value, ErrorType>.pipe()
227+
let (left, right) = transform(pipeSignal)
228+
let coldLeft = ColdSignal<U, F> { observer in
229+
left.add(observer: observer)
230+
return self.coldSignal.startHandler(pipeObserver)
231+
}
232+
let coldRight = ColdSignal<U, F> { observer in
233+
right.add(observer: observer)
234+
return self.coldSignal.startHandler(pipeObserver)
235+
}
236+
return (coldLeft, coldRight)
237+
}
238+
223239
/// Maps each value in the signal to a new value.
224240
public func map<U>(_ transform: @escaping (Value) -> U) -> ColdSignal<U, ErrorType> {
225241
return lift { $0.map(transform) }
@@ -235,6 +251,13 @@ public extension ColdSignalType {
235251
return lift { $0.filter(predicate) }
236252
}
237253

254+
/// Splits the signal into two signals. The first signal in the tuple matches the
255+
/// predicate, the second signal does not match the predicate
256+
public func partition(_ predicate: @escaping (Value) -> Bool)
257+
-> (ColdSignal<Value, ErrorType>, ColdSignal<Value, ErrorType>) {
258+
return lift { $0.partition(predicate) }
259+
}
260+
238261
/// Aggregate values into a single combined value. Mirrors the Swift Collection
239262
public func reduce<T>(initial: T, _ combine: @escaping (T, Value) -> T) -> ColdSignal<T, ErrorType> {
240263
return lift { $0.reduce(initial: initial, combine) }

Sources/Signal.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,36 @@ public extension SignalType {
284284
}
285285
}
286286

287+
/// Splits the signal into two signals. The first signal in the tuple matches the
288+
/// predicate, the second signal does not match the predicate
289+
public func partition(_ predicate: @escaping (Value) -> Bool) -> (Signal<Value, ErrorType>, Signal<Value, ErrorType>) {
290+
let left = Signal<Value, ErrorType> { observer in
291+
return self.on { (event: Event<Value, ErrorType>) -> Void in
292+
guard let value = event.value else {
293+
observer.sendEvent(event)
294+
return
295+
}
296+
297+
if predicate(value) {
298+
observer.sendNext(value)
299+
}
300+
}
301+
}
302+
let right = Signal<Value, ErrorType> { observer in
303+
return self.on { (event: Event<Value, ErrorType>) -> Void in
304+
guard let value = event.value else {
305+
observer.sendEvent(event)
306+
return
307+
}
308+
309+
if !predicate(value) {
310+
observer.sendNext(value)
311+
}
312+
}
313+
}
314+
return (left, right)
315+
}
316+
287317
/// Aggregate values into a single combined value. Mirrors the Swift Collection
288318
public func reduce<T>(initial: T, _ combine: @escaping (T, Value) -> T) -> Signal<T, ErrorType> {
289319
return Signal { observer in

0 commit comments

Comments
 (0)