title: RxJava2 idiom for combining two Maybes
body: <p>Given two <code>Maybe</code> values, how can I combine them into a single <code>Maybe</code> that will either:</p>
  
  <ol>
  <li>call <code>onSuccess()</code> whenever either of the source <code>Maybes</code> calls <code>onSuccess</code></li>
  <li>call <code>onComplete()</code> whenever <em>both</em> of the source <code>Maybes</code> call <code>onComplete()</code>?</li>
  </ol>
  
  <p>(Cf. <code>Option.orElse()</code> in <a href="https://www.scala-lang.org/api/2.12.2/scala/Option.html#orElse[B%3E:A](alternative:=%3EOption[B]):Option[B]" rel="nofollow noreferrer">Scala</a> or <a href="https://static.javadoc.io/io.vavr/vavr/0.9.2/io/vavr/control/Option.html#orElse-io.vavr.control.Option-" rel="nofollow noreferrer">Vavr</a>.)</p>
  
  <p>E.g., assuming the existence of a <code>combine()</code> method that does what I want:</p>
  
  <pre><code>combine(Maybe.just(a), Maybe.empty())  ≍ Maybe.just(a)
  combine(Maybe.empty(), Maybe.just(b))  ≍ Maybe.just(b)
  combine(Maybe.empty(), Maybe.empty())  ≍ Maybe.empty()
  combine(Maybe.never(), /*anything*/ )  ≍ /*the thing*/ 
  combine(/*anything*/,  Maybe.never())  ≍ /*the thing*/ 
  </code></pre>
  
  <p>At first I thought <code>amb()</code> &amp; family were what I was looking for, but that completes as soon as either source <code>Maybe</code> completes, meaning if the first <code>Maybe</code> completes without a value, you never get the value from the second <code>Maybe</code>.</p>
  
  <p>Right now I'm using</p>
  
  <pre><code>Maybe.mergeArray(m1, m2).firstElement()
  </code></pre>
  
  <p>which seems to do what I want, but I’m not certain it’s correct and I’m not certain it’s the cleanest way to do it. (For instance, if there’s some delay, will it call <code>onSuccess()</code> immediately when one or the other source does, or will it wait for both <code>onComplete()</code>s?)</p>
  
  <p>Is this correct? Is there a more idiomatic approach?</p>
  
  <hr>
  
  <p><strong>ETA:</strong> I'm happy taking the first value; I don't need to wait for both to complete:</p>
  
  <pre><code>combine(Maybe.just(a), Maybe.just(b))  ≍ Maybe.just(/* a or b, don't care */)
  </code></pre>
  
  <p>(I can imagine situations in which I might prefer one or the other and want to indicate that by order of the arguments, but in that situation I suspect sequential would be better than parallel.)</p>