Flapjax Worlds

110 views
Skip to first unread message

Joe Gibbs Politz

unread,
Jan 9, 2012, 10:03:58 AM1/9/12
to fla...@googlegroups.com
Often, we have a number of event streams, and we would like to
abstract them over some common, shared data. This is called World
programming in Racket [1].

I think Flapjax can benefit from this idea. Here's an example of this pattern:

https://github.com/jpolitz/fj-world/blob/master/tests.html

And an implementation of the pattern:

https://github.com/jpolitz/fj-world/blob/master/flapjax-world.js

I've put type annotations in comments to help explain what's going on.
A World in Racket is a list of functions that abstract over shared
data, but each also has other, specific data related to the type of
event it handles, and this data is used to update the shared state.
I've implemented the merge-and-collect pattern in the world() function
multiple times before, and this abstraction will help me write similar
things in the future.

World programs in Racket come with other bells and whistles, but this
abstraction is the core of what's going on.

Parting thought: What should happen when the initial value given to
world() is itself a behavior?

Cheers,
Joe P.

[1] http://docs.racket-lang.org/teachpack/2htdpuniverse.html?q=world#%28form._%28%28lib._2htdp/universe..rkt%29._big-bang%29%29

Danny Yoo

unread,
Jan 9, 2012, 2:53:56 PM1/9/12
to fla...@googlegroups.com
> Parting thought: What should happen when the initial value given to
> world() is itself a behavior?


If the world itself is an opaque value to the "operating system", then
shouldn't it make no difference what type the world is?

Joe Gibbs Politz

unread,
Jan 9, 2012, 3:49:42 PM1/9/12
to fla...@googlegroups.com

I'm pretty sure this is the right answer.

I had though about interpreting the intial values all as behaviors (the common
case of existing worlds would be a constantB), and attempting some sort of
lifting of the handlers over the behavior as well as over the eventstream
parameters. I was prompted by reading Flapjax's documentation, where many
parameterized values can also be time-varying. I'm mostly convinced that
constant values is the right choice here, however, for the reason you give.

Arjun Guha

unread,
Jan 10, 2012, 10:37:32 PM1/10/12
to fla...@googlegroups.com
You should commit this to Flapjax. Use the extractEvent2E branch, which is where new development is happened. I'd like to make a release this semester and we should include this as a better way of doing state machines.


Arjun Guha

unread,
Feb 21, 2012, 1:19:48 PM2/21/12
to fla...@googlegroups.com
Joe,

I think you can simplify these lines in your code:


   var to_draw = w.liftB(P);
    insertDomB(to_draw, getObj('timer'), 'end');

to just:

    insertDomB(P(w), getObj('timer'), 'end');

Arjun

Joe Gibbs Politz

unread,
Feb 21, 2012, 2:00:22 PM2/21/12
to fla...@googlegroups.com
Indeed. The variable and its name were added to make the world analogy clear.

Joe Gibbs Politz

unread,
Sep 26, 2012, 3:20:06 PM9/26/12
to fla...@googlegroups.com
This idea shows up in Tangle, too:

http://worrydream.com/Tangle/

Brian Craft

unread,
Aug 13, 2013, 6:24:39 AM8/13/13
to fla...@googlegroups.com
This pattern looks useful, but I'm not happy with creating a closure on every event. Here's a version w/o the closures, which I think is equivalent apart from the startsWith (which can be added by the caller):

    function worldE(init, handlers) {
        var r = fj.receiverE();
        fj.forEach(function(h) {
            h[0].mapE(function (ev) {
                r.sendEvent(init = h[1](init, ev));
            });
        }, handlers);
        return r;
    }


Reply all
Reply to author
Forward
0 new messages