title: Implementing sequence : List<Observable<T>> -> Observable<List<T>>
body: <p>For every type T,
  having a list of n Observables of Ts, I'd like to build an Observable emitting a list of n Ts each time one of the original Observables emits something.
  This is often refereed as the 'sequence' operator in the functional literature.</p>
  
  <p>Desired behaviour example in a pseudo-syntax:</p>
  
  <pre><code>val o1 = BehaviourSubject.create(true)
  val o2 = BehaviourSubject.create(false)
  val listOfObservables = [o1,o2]
  
  val observableOfList = sequence(listOfObservables)
  
  observableOfList.subscribe(print)
  
  o2.onNext(true)
  
  // Expected output:
  // [true, false]
  // [true, true]
  </code></pre>
  
  <p>I wrote the following naive implementation in java, which misbehave:</p>
  
  <pre><code>public static &lt;T&gt; Observable&lt;List&lt;T&gt;&gt; sequence(List&lt;Observable&lt;T&gt;&gt; from) {
    return fold(from, Observable.&lt;List&lt;T&gt;&gt;never().startWith(new ArrayList&lt;T&gt;()),
              (arrayListObservable, observable) -&gt; {
      return Observable.combineLatest(arrayListObservable, observable, (ts, t) -&gt; {
        ts.add(t);
        return ts;
      });
    });
  }
  
  public static &lt;F, T&gt; T fold(final Iterable&lt;? extends F&gt; elements, final T zero, final Func2&lt;T, F, T&gt; f) {
    T currentValue = zero;
    for (final F element : elements) {
      currentValue = f.call(currentValue, element);
    }
    return currentValue;
  }
  
  // Actual output
  // [true, false]
  // [true, false, true]
  </code></pre>
  
  <p>Somehow I need to rebuild the resulting list instead of appendiong the new values to the existing one. How would you guys/ladies go with it ?</p>
  
  <p>Thanks for you time and future answers !</p>