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
    Do you mean anything more complex than signals.some(signal => signal) (JS used for familiarity, other languages typically use any)? For AND one typically sees signals.every(signal => signal) (other languages typically use all). 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.
    – zzzzBov
    Aug 25 '16 at 18:47
  • not familiar with the JS syntax, maybe there is something built into the framework, do you have a link I could look at? Mainly I'm looking for a solution involving methods specified at reactivex.io. On a side note I'm using Reactive Cocoa and RxJava Aug 25 '16 at 18:53
  • Seems like there is a 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. Aug 25 '16 at 19:00

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. Oct 10 '16 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.