Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Page 2 - Flipping Flapjax onto JavaScript
Flipping Flapjax onto JavaScript
By Lynn Greiner

Rate This Article: Add This Article To:

Flipping Flapjax onto JavaScript - ' For Example'
( Page 2 of 2 )

One example from the Flapjax Tutorial illustrates the concept nicely. (For example, by using Flapjax's templating syntax, shown here, you can embed the code in a standard Web page). This fragment will continuously display the system time, in seconds, recalculating whenever the clock changes:

<p>
The time in seconds is {! Math.floor(timer_b(100) / 1000) !}.
</p>

The function timer_b creates something called a behavior – a value that changes over time. Behaviors are central to Flapjax. This particular behavior returns a value every 100 milliseconds; however, it is returning a microsecond value, so the division by 1000 smooths out the display. You can run versions of the program with and without the division on the tutorial page to see the difference.

Notice, too, that any expression whose values depend on a behavior becomes a behavior itself, so the calculations on the returned value occur automatically each time timer_b is updated.

ADVERTISEMENT

For phenomena that don't occur continuously, such as keystrokes or mouse clicks, Flapjax provides event streams. Polling for values here is not an effective tactic because we don't know if there will even be another event. Instead, a new value triggers the code.

In this simple example from the tutorial, the goal is to find the length of whatever text string is entered into a box.

Text buffer: <input type="text" id="toLength"></input>
<p>
Length: {! extractValue_e('toLength').transform(
             function (s) {return s.length; }) !}
<p>

The first line defines the box. Then extractValue_e creates an event stream of all events involving that box, and the transform method iterates the event stream of the string into a stream of the lengths of the input text.

Events and behaviors are related, of course; an event stream can be converted to a behavior, if it has an initial value (the behavior would always contain the most recent event in the stream), and behaviors can become event streams. Flapjax offers primitives that will do the conversion for you.

There are logical places where one or the other is more appropriate, however. The tutorial makes the following recommendation:

"As a rule-of-thumb, use behaviors to represent internal state and when the computation that expresses a dependency between two values won't run for too long. When the state is in some external medium, odds are the communications channel can be unpredictable, making event streams more suitable. Similarly, if a computation has the potential to take too long and you may need to time it out, it's better to use event stream and merge in timeout behavior."

One thing you won't see in Flapjax is callbacks, and that's deliberate. The authors feel that, although callbacks have a useful place in programming structure, their use can also be a drawback. They suggest that programmers think more about the data model, and let the language do the work of maintaining consistency.

Want to know more? You can download the compiler, run through the tutorial and view demos at www.flapjax-lang.org.



 
 
>>> More Languages Articles          >>> More By Lynn Greiner
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.