title: RXJS Scan - withLatestFrom another observable
body: <p>I'm trying to work out how to use scan to derive a new state whenever my input observable emits a new value, but I can't seem to get it working.</p>
  
  <p>I want to output a new State every time the input$ observable emits a new value, but it should be derived from the current value of state$.</p>
  
  <p>Can anyone suggest how I can fix this? I have a feeling I've got the wrong idea altogether :-)</p>
  
  <p>My code looks something like this:</p>
  
  <pre><code>const stateReducer = (state$: Observable&lt;State&gt;, input$: Observable&lt;Input&gt;) =&gt; {
    state$ = state$.pipe( startWith(DEFAULT_STATE) );
  
    const foo$: Observable&lt;State&gt; = input$.pipe(
      filter((input) =&gt; isFoo(input)),
      withLatestFrom(state$),
      scan((acc, ([input, state]) =&gt; {
          //returns derived state
      });
  
     const bar$: Observable&lt;State&gt; = input$.pipe(
      filter((input) =&gt; isBar(input)),
      withLatestFrom(state$),
      scan((acc, ([input, state]) =&gt; {
          //returns derived state
      });
  
     return merge(
       foo$,
       bar$
     );
  }
  </code></pre>