Restrictions on Evenstream functions?

5 views
Skip to first unread message

Paul Meier

unread,
Dec 20, 2009, 8:30:00 PM12/20/09
to fla...@googlegroups.com
Hello,

     I've been working on a few lines of code for several hours now, but I'm confused by the Flapjax semantics.  My question involves scoping rules of Flapjax expressions, along with any restrictions on the functions.

     For example, suppose I want to create a thunk that runs an EventStream expression, take the first value fired by the EventStream, and memoize that first value for later use.  For the purposes this example, the EventStream in question will be a trivial oneE(6) one.  I've tried several variations of this:

------------------------------------------
function() {
    var value        = false;
    var computed = false;
    if (computed) {
        return value;
    else {
        value = onceE(oneE(6)).mapE( function(x) {  value = x; });
        computed = true;
        return value;
    }
}
------------------------------------------

    When I run this, I always get the initialized value, false, rather than 6.

    The onceE() allows me to generalize this to any Flapjax expression that may fire many times (as I only ever want the first value, for the purposes of my application).  'Several variations' includes trivial and desperate Javascript changes ("maybe it makes a difference if the expression is 'return value = x;' instead of the simple mutation 'value = x;') as well as guesses at the Flapjax implementation (the reference page notes the oneE() doesn't fire immediately....  I've tried changing the oneE EventStream to

var receiver = receiverE();
.... expressions with receiver instead of oneE(6)...
sendEvent(receiver, 6);

in an attempt to force propagation).

    Can anything like this be done?  Is it possible, as non-idiomatic as it sounds, to simply take the car of an EventStream?  Does it have to do with scoping of the variable 'value'?

    Any help would be appreciated.  Thanks for your time ^_^

-Paul

Aran Donohue

unread,
Dec 20, 2009, 9:17:56 PM12/20/09
to fla...@googlegroups.com
I'm not sure I understand exactly what you're trying to do. But I think you might be wanting to put "value" and "computed" in a closure.



var desiredFunc = function() {

var value        = false;
var computed = false;

return function() {

    if (computed) {
        return value;
    else {
        onceE(oneE(6)).mapE( function(x) {  value = x; });
        computed = true;
        return value;
    }
}


}();

I apologize if I'm misunderstanding your problem.

Aran

Paul Meier

unread,
Dec 20, 2009, 10:47:35 PM12/20/09
to fla...@googlegroups.com
Ah!  Thanks, another typo (my work is on another computer, I was typing it from memory as I was leaving for dinner), 'value' and 'computed' are declared outside the closure I returned. 

That is closer to what its trying to do (in that 'value' and 'computed' are declared outside the closure), but ultimately my problem is still the return value from calling said closure.  When it returns 'value', it is returns false, even after the onceE eventstream is programmed to set it to 6.

Thanks for your response, if my problem is still unclear (and anyone wants to take a crack at it) I'd be happy to clarify.  I'll keep working on it, and post when/if I resolve it.

Thanks again ^_^

-Paul

Thanks for your response though, I'll

John Freeman

unread,
Dec 21, 2009, 1:19:48 AM12/21/09
to fla...@googlegroups.com
Paul Meier wrote:
> function() {
> var value = false;
> var computed = false;
> if (computed) {
> return value;
> else {
> value = onceE(oneE(6)).mapE( function(x) { value = x; });

This creates an event stream that will fire its first event /after/ this
function has returned. oneE queues an event; it does not fire it
immediately.

> computed = true;
> return value;

This returns variable "value" with value "false" before the event fires.

> }
> }

Hope this helps,
John

Reply all
Reply to author
Forward
0 new messages