I'm would like to know your opinion of what is an ideomatic way writing a OR function in functional reactive programming.
That is, if I have x number of signals which can return a truthy or falsy value, how can I evaluate if at least one signal is truthy, or all signals are falsy? Finally I would like to send the result (true/false) as a result.
I can think of a number of ways of accomplishing this but they all feel bloated and over engineered.
1 Answer 1
It sounds like a basic combineLatest operation to me. In RxSwift that would be something like:
func anyTrue(streams: [Observable<Bool>]) -> Observable<Bool> {
return streams.combineLatest { 0ドル.contains(true) }
}
The above will begin emitting values after all the inputs have emitted at least one value. As long as at least one of the streams have emitted true, it will emit true, otherwise it will emit false.
In ReactiveCocoa, this would be:
func anyTrue(streams: [Signal<Bool, NoError>]) -> Signal<Bool, NoError> {
return combineLatest(streams).map { 0ドル.contains(true) }
}
-
Yes, figured it out eventually.Erik Johansson– Erik Johansson2016年10月10日 07:02:37 +00:00Commented Oct 10, 2016 at 7:02
Explore related questions
See similar questions with these tags.
signals.some(signal => signal)
(JS used for familiarity, other languages typically useany
)? ForAND
one typically seessignals.every(signal => signal)
(other languages typically useall
). In both these examples I'm assuming that the collection contains the truthy/falsey values, but it could certainly be replaced by any function that maps to a property of the individual signal.All
/Every
in the Rx specification. Can't find anything similar in Reactive Cocoa but I'll check the implementation. Thanks for pointing me in the right direction.