Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Questions</h2> <p><strong>Question 1</strong></p> <blockquote> <p>I can't just do what i did above when trying to add int's together.</p> </blockquote> <p>Well, that's actually the point. In reactive programming, you don't want to imperatively add two numbers together, instead you want to define the new number in terms of the other numbers. Therefore, <code>c = a + b</code>, such <code>c</code> is <em>always</em> equal to <code>a + b</code>, even when <code>a</code> or <code>b</code> changes: <code>c</code> is reactive in respect to <code>a</code> and <code>b</code>.</p> <pre><code>var a = new BehaviorSubject(3); var b = new BehaviorSubject(1); var c = Rx.Observable.combineLatest(a, b, function(vals) { return vals[0] + vals[1]; }); </code></pre> <p><strong>Question 2</strong></p> <blockquote> <p>I was wondering how exactly one would implement the example given in the 2nd highest rated answer.</p> </blockquote> <p><strong>Simplest answer</strong> lists and higher-order functions in haskell.</p> <p><strong>Answer you don't want</strong> Functional-reactive programming goes against everything you've learned in imperative programming, and you're going to have to re-learn how to <em>do</em> things if you want to do pure functional reactive programming. If you don't, you're going to end up making all kinds of dependancy tracking libraries like <a href="https://github.com/SteveSanderson/knockout">KnockoutJS</a>, when you could do the same thing in a couple of hundred lines with something like <a href="https://github.com/cwharris/rxjs-splash">RxJS-Splash</a>, if you used FRP to begin with. (note how Splash is based on Rx, which is reusable code, and Knockout is purely implementation specific code).</p> <p>FRP has a concept of <em>events</em> and <em>time</em>, whereas dependency tracking only has concepts of <em>values</em> and <em>changes</em>. Functional-reactive code has been around just as long as imperative code. It's not "built on top of imperative code." (yes it still compiles to assembly... not the point), it's <strong>fundamentally</strong> different in concept.</p> <h2>Example</h2> <p>Using Microsoft's Reactive Extensions for JavaScript (<a href="http://github.com/Reactive-Extensions/RxJS">RxJS</a>)</p> <p>Keep in mind, Rx is available in a lot of languages now, including native <a href="http://rx.codeplex.com/">C++</a>.</p> <p><strong>Direct Port of Example</strong></p> <pre><code>var moves = $(document).onAsObservable('mousemove') .map(function(e) { return { x: e.pageX, y: e.pageY }; }); var xs = moves.map(function(move) { return move.x; }); var ys = moves.map(function(move) { return move.y; }); var minXs = xs.map(function(x) { return x - 16; }); var minYs = ys.map(function(y) { return y - 16; }); var maxYs = xs.map(function(x) { return x + 16; }); var maxYs = ys.map(function(y) { return y + 16; }); var boundingRect = Rx.Observable.combineLatest(minXs, minYs, maxXs, maxYs) .map(function(vals) { var minX = vals[0]; var minY = vals[1]; var maxX = vals[2]; var maxY = vals[3]; return new Rectangle(minX, minY, maxX, maxY); }); </code></pre> <p><strong>Simplified Port</strong></p> <p>Since the rectangle is defined only in terms of one dependent value (or event), then you can simplify this to the following:</p> <pre><code>var boundingRect = $(document).onAsObservable('mousemove') .map(function(e) { var x = e.pageX; var y = e.pageY; return new Rectangle(x - 16, y - 16, x + 16, y + 16); }); </code></pre> <p><strong>Using It</strong></p> <p>At which point you can use it to compose other observable sequences (values which changes over time).</p> <pre><code>var area = boundingRect.map(function(rect) { return rect.getSize(); }); </code></pre> <p>Or subscribe to it directly.</p> <pre><code>boundingRect.subscribe(function (rect) { // perform some action with the rect each time it changes. console.log(rect); }); </code></pre> <p><strong>But that's only when it changes!</strong></p> <p>What if we want the latest value as soon as we subscribe, rather than having to wait for the rectangle to change again? Well, that's where <code>BehaviorSubject</code>s come in.</p> <pre><code>var rects = new Rx.BehaviorSubject(new Rectangle(-1, -1, 1, 1)); rects.subscribe(function(rect) { // this would happen twice in this example. // Once for the initial value (above), and once when it is changed later (below). console.log(rect); }); rects.onNext(new Rectangle(-1, -1, 1, 1)); </code></pre> <p><strong>But that's not the original observable, and it's not very functional...</strong></p> <p>Here's how to do the same thing with the original observable using some built in functionality to change on Observable to a act like a BehaviorSubject...</p> <pre><code>var rectsAndDefault = rects.startWith(new Rectangle()); // just give it an initial value rectsAndDefault.subscribe(function(rect) { console.log(rect); // happens once for the "startWith" rectangle, and then again for all subsequent changes }) </code></pre> <p>Again, FRP is different. It's good different, but it's a big undertaking to learn. You'll know you've started getting it when it starts blowing your mind.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload