0

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.

asked Aug 25, 2016 at 8:12
3
  • 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. Commented Aug 25, 2016 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 Commented Aug 25, 2016 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. Commented Aug 25, 2016 at 19:00

1 Answer 1

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) }
}
answered Oct 10, 2016 at 0:41
1
  • Yes, figured it out eventually. Commented Oct 10, 2016 at 7:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.