Sunday, August 24, 2008

Arrows in Javascript

Last year, I mentioned a lesson I learned from one of my CS professors many years ago:

He began by recommending every project start by hiring a mathematician for a week or two to study the problem. If a mathematician could find an interesting property or algorithm, then it would be money well spent, and drastically reduce the time/money/effort needed to develop a system.

A few weeks ago, I came across a paper about Arrows in Javascript. It's an interesting adaptation of arrows to do asynchronous, event driven programming in Javascript. It makes an excellent example of this dictum.

Here's the problem in a nutshell. There's a very interesting language lurking underneath Javascript. It's an object oriented language with namespaces, closures, and dynamic typing. Doug Crockford has written and presented these concepts over the last few years.

As good as the Javascript is on its own merits (albeit abused and misunderstood), it is generally used in a pretty horrendous environment. Browsers have compatibility bugs in their language support and the cross platform APIs they expose. Most of all, browsers execute Javascript in a single-threaded manner. This makes asynchronous programming difficult, especially when there are two interwoven threads of execution, like drag and drop and dynamic page updates.

This paper by a team at UMD starts by bringing continuation passing to Javascript, and implementing arrows on top of that. Once arrows are available, building a library for asynchronous event programming is much easier than brute force chaining of event callbacks. For example, drag and drop programming can be specified common method chaining:

.run ()

The plumbing inside the library can be a little hairy, especially if you find CPS or arrows befuddling. But the exposed API is quite elegant and appears to be easy to use. It'll be interesting to watch the Javascript library space to see when and where it pops up.


Bill Mill said...

is the code available somewhere? A brief scan of the paper didn't make it obvious.

James Long, code expander said...

You should see Narrative JS. I really appreciate it's syntax.

It essentially brings frame-based continuations to the tables and builds itself off of it.

troelskn said...

There is already a javascript port of Deferred - part of the Python-framework Twisted - in Mochikit.

Anonymous said...

You might be interested in an earlier JS project, Flapjax, which is based on FRP and more fleshed out for the web. Some flavors of FRP can be derived from arrows - but you get extra goodness from the more specific semantics representing the domain better.

blasdelf said...

"browsers execute Javascript in a single-threaded manner"

You don't necessarily need parallelism (native threads), concurrency is what really helps, at least algorithmically.

Brendan Eich, the author of JavaScript née LiveScript (originally a Scheme implementation), has said that JavaScript will have threading "over his dead body". That's probably a good thing, seeing as how poorly something like pthread would fit in JS, but some concurrency primitives in the language itself would definitely be helpful.

shapr said...

Looks like CPS has been available in Javascript via an Appel trampoline: see;item=js.cps.Appel

Adam Turoff said...

Bill Mill - I didn't see any reference to an implementation either. But today, this project popped up (via). There's a paddleball type demo that only works in Firefox and Opera at the moment (source). It isn't pretty (and the code is a little rough), but it demonstrates the idea.