title: Circular dependencies of EventStreams in FRP
body: <p><em>All examples uses <code>Ramda</code> as <code>_</code> (it's clear what methods do in examples contexts) and <code>kefir</code> as <code>frp</code> (almost same API as in bacon.js)</em></p>
  
  <p>I have a stream, that describes change of position.</p>
  
  <pre><code>var xDelta = frp
      .merge([
          up.map(_.multiply(1)),
          down.map(_.multiply(-1))
      ])
      .sampledBy(frp.interval(10, 0))
      .filter();
  </code></pre>
  
  <p>It emits <code>+1</code> when I press <code>UP</code> key, and <code>-1</code> on <code>DOWN</code>.</p>
  
  <p>To get position I <code>scan</code> this delta</p>
  
  <pre><code>var x = xDelta
      .scan(_.add)
      .toProperty(0);
  </code></pre>
  
  <p>That's work as expected. But I want to limit value of <code>x</code> from <code>0</code> to <code>1000</code>.</p>
  
  <p>To solve this problem I found two solution:</p>
  
  <ol>
  <li><p>Change function in <code>scan</code></p>
  
  <pre><code>var x = xDelta.scan(function (prev, next) {
      var newPosition = prev + next;
      if (newPosition &lt; 0 &amp;&amp; next &lt; 0) {
          return prev;
      }
      if (newPosition &gt; 1000 &amp;&amp; next &gt; 0) {
          return prev;
      }
      return newPosition;
  }, 0);
  </code></pre></li>
  </ol>
  
  <p>It looks Ok, but later, as new rules will be introduced, this method will grow. So I mean it doesn't look composable and FRPy.</p>
  
  <ol start="2">
  <li><p>I have <code>current</code> position. And <code>delta</code>. I want to apply <code>delta</code> to <code>current</code>, only if <code>current after applying</code> will not be out of limits.</p>
  
  <ul>
  <li><code>current</code> depends on <code>delta</code></li>
  <li><code>delta</code> depends on <code>current after applying</code></li>
  <li><code>current after applying</code> depends on <code>current</code></li>
  </ul>
  
  <p>So it looks like circular dependency. But I solved it using <code>flatMap</code>.</p>
  
  <pre><code>var xDelta = frp
      .merge([
          up.map(_.multiply(1)),
          down.map(_.multiply(-1))
      ])
      .sampledBy(frp.interval(10, 0))
      .filter();
  
  var possibleNewPlace = xDelta
      .flatMap(function (delta) {
          return x
              .take(1)
              .map(_.add(delta));
      });
  
  var outOfLeftBoundFilter = possibleNewPlace
      .map(_.lte(0))
      .combine(xDelta.map(_.lte(0)), _.or);
  
  var outOfRightBoundFilter = possibleNewPlace
      .map(_.gte(1000))
      .combine(xDelta.map(_.gte(0)), _.or);
  
  var outOfBoundFilter = frp
      .combine([
          outOfLeftBoundFilter,
          outOfRightBoundFilter
      ], _.and);
  
  var x = xDelta
      .filterBy(outOfBoundFilter)
      .scan(_.add)
      .toProperty(0);
  </code></pre>
  
  <p>You can see full code example at <a href="https://github.com/iofjuupasli/capture-the-sheep-frp/tree/master" rel="nofollow" title="iofjuupasli/capture-the-sheep-frp">iofjuupasli/capture-the-sheep-frp</a></p>
  
  <p>And it's working demo <a href="http://iofjuupasli.github.io/capture-the-sheep-frp/" rel="nofollow">gh-pages</a></p>
  
  <p>It works, but using circular dependencies is probably anti-pattern.</p></li>
  </ol>
  
  <p>Is there a better way to solve circular dependency in FRP?</p>
  
  <h1>The second more general question</h1>
  
  <p>With <code>Controller</code> it's possible to read some values from two <code>Model</code> and depending on it's values update both of them.</p>
  
  <p>So dependencies looks like:</p>
  
  <pre><code>              ---&gt; Model
  Controller ---|
                ---&gt; Model
  </code></pre>
  
  <p>With FRP there is no <code>Controller</code>. So <code>Model</code> value should be declaratively calculated from other <code>Model</code>. But what if <code>Model1</code> calculating from another <code>Model2</code> which is the same, so <code>Model2</code> calculates from <code>Model1</code>?</p>
  
  <pre><code>Model -----&gt;
        &lt;----- Model
  </code></pre>
  
  <p>For example two players with collision detection: both players have <code>position</code> and <code>movement</code>. And <code>movement</code> of first player depends on <code>position</code> of second, and vice versa.</p>
  
  <p>I'm still newbie in all this stuff. It's not easy to start think in declarative FRP style after years of imperative coding. Probably I'm missing something.</p>