title: For each in list, do operation until condition is met
body: <p>Consider the following code: </p>
  
  <pre><code>void tryToOpenSafe() {
      getCorrectSafeCombination().subscribe(combination -&gt; System.out.println("Correct combination is " + combination));
  }
  
  Maybe&lt;Integer&gt; getCorrectSafeCombination() {
      return getPossibleCombinations()
              .toObservable()
              .flatMapIterable(combinations -&gt; combinations)
              .flatMap(combination -&gt; tryToOpenSafeWithCombination(combination).toObservable()
                      .map(isCorrect -&gt; new CombinationCheckResult(combination, isCorrect)))
              .filter(result -&gt; result.isCorrect)
              .map(result -&gt; result.combination)
              .firstElement();
  
  }
  
  Single&lt;List&lt;Integer&gt;&gt; getPossibleCombinations() {
      final List&lt;Integer&gt; combinations = Arrays.asList(123, 456, 789, 321);
      return Single.just(combinations);
  }
  
  // this is expensive
  final Single&lt;Boolean&gt; tryToOpenSafeWithCombination(int combination) {
      System.out.println("Trying to open safe with " + combination);
      final boolean isCorrectCombination = combination == 789;
      return Single.just(isCorrectCombination);
  }
  </code></pre>
  
  <p>I receive a list of possible "combinations" (integer) for a safe I want to open. Only one combination is the correct one of course. </p>
  
  <p>With my current approach, <code>getCorrectSafeCombination()</code> will deliver the first correct combination it found; but it will try all the combinations nonetheless. </p>
  
  <p>This is in efficient though: as soon as the correct combination is found, there is no need to try the others. </p>
  
  <p>How can this be done with Rx?</p>