Added partition for signal and cold signal
This commit is contained in:
parent
706f6704dd
commit
16d8000d6a
|
@ -220,6 +220,22 @@ public extension ColdSignalType {
|
|||
}
|
||||
}
|
||||
|
||||
public func lift<U, F>(_ transform: @escaping (Signal<Value, ErrorType>) -> (Signal<U, F>, Signal<U, F>))
|
||||
-> (ColdSignal<U, F>, ColdSignal<U, F>)
|
||||
{
|
||||
let (pipeSignal, pipeObserver) = Signal<Value, ErrorType>.pipe()
|
||||
let (left, right) = transform(pipeSignal)
|
||||
let coldLeft = ColdSignal<U, F> { observer in
|
||||
left.add(observer: observer)
|
||||
return self.coldSignal.startHandler(pipeObserver)
|
||||
}
|
||||
let coldRight = ColdSignal<U, F> { observer in
|
||||
right.add(observer: observer)
|
||||
return self.coldSignal.startHandler(pipeObserver)
|
||||
}
|
||||
return (coldLeft, coldRight)
|
||||
}
|
||||
|
||||
/// Maps each value in the signal to a new value.
|
||||
public func map<U>(_ transform: @escaping (Value) -> U) -> ColdSignal<U, ErrorType> {
|
||||
return lift { $0.map(transform) }
|
||||
|
@ -235,6 +251,13 @@ public extension ColdSignalType {
|
|||
return lift { $0.filter(predicate) }
|
||||
}
|
||||
|
||||
/// Splits the signal into two signals. The first signal in the tuple matches the
|
||||
/// predicate, the second signal does not match the predicate
|
||||
public func partition(_ predicate: @escaping (Value) -> Bool)
|
||||
-> (ColdSignal<Value, ErrorType>, ColdSignal<Value, ErrorType>) {
|
||||
return lift { $0.partition(predicate) }
|
||||
}
|
||||
|
||||
/// Aggregate values into a single combined value. Mirrors the Swift Collection
|
||||
public func reduce<T>(initial: T, _ combine: @escaping (T, Value) -> T) -> ColdSignal<T, ErrorType> {
|
||||
return lift { $0.reduce(initial: initial, combine) }
|
||||
|
|
|
@ -284,6 +284,36 @@ public extension SignalType {
|
|||
}
|
||||
}
|
||||
|
||||
/// Splits the signal into two signals. The first signal in the tuple matches the
|
||||
/// predicate, the second signal does not match the predicate
|
||||
public func partition(_ predicate: @escaping (Value) -> Bool) -> (Signal<Value, ErrorType>, Signal<Value, ErrorType>) {
|
||||
let left = Signal<Value, ErrorType> { observer in
|
||||
return self.on { (event: Event<Value, ErrorType>) -> Void in
|
||||
guard let value = event.value else {
|
||||
observer.sendEvent(event)
|
||||
return
|
||||
}
|
||||
|
||||
if predicate(value) {
|
||||
observer.sendNext(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
let right = Signal<Value, ErrorType> { observer in
|
||||
return self.on { (event: Event<Value, ErrorType>) -> Void in
|
||||
guard let value = event.value else {
|
||||
observer.sendEvent(event)
|
||||
return
|
||||
}
|
||||
|
||||
if !predicate(value) {
|
||||
observer.sendNext(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return (left, right)
|
||||
}
|
||||
|
||||
/// Aggregate values into a single combined value. Mirrors the Swift Collection
|
||||
public func reduce<T>(initial: T, _ combine: @escaping (T, Value) -> T) -> Signal<T, ErrorType> {
|
||||
return Signal { observer in
|
||||
|
|
Loading…
Reference in New Issue