P16: a blog by Matt Kangas home archive
02 Jan 2008

Madcap idea: "Funky (func-y) JavaScript"

I haven't exactly finished setting this blog up yet, but an idea just occurred to me, and rather than jot it in my paper notebook, I thought I'd post it publicly for y'all to see.

This is probably a harebrained idea. I'm sure I'll see some flaws in it tomorrow, and you will see them even sooner.

Madcap idea: "Funky JavaScript" (FunkyJS):

  1. Define a strictly functional subset of JavaScript – code which, if translated, could run within the Erlang virtual machine. (Key restriction: no mutable variables. All variables must be write-once, as in Erlang.)
  2. Build a JavaScript parser in Erlang
  3. Try to generate Erlang bytecodes and run the result!

I'm calling it "Funky" because it's func -> functional. And weird too.

Yeah, I'm demented to even consider this. But it falls right at the intersection of a whole bunch of problem domains that I've been looking at:

  1. CouchDb is implemented in Erlang, and uses JavaScript as its query language. It uses Mozilla Spidermonkey as its JS engine, but as you can see on their dev blog, this has its downsides.
  2. Erlang holds incredible promise for running distributed server-side programs. But the language is very different from anything else in the standard web-development toolbox (Perl,PHP,C,Java,etc).
  3. AJAX is a cross-platform UI juggernaut. If you want to make a web-connected application that "runs everywhere", and can overcome the cross-platform quirks, you build it on AJAX. It works today on iPhone and Maemo and Android. Need I say more?
  4. JavaScript is the "J" in "AJAX". It's everywhere.

Erlang is very capable on the server. JavaScript is ubiquitous on the client side. If we could bridge this gap, perhaps some really interesting apps could emerge?

How about a multiplayer game like Scrabble or Battleship that works as a Facebook app, on iPhone, and every desktop web browser? How about massively-multiplayer, with tournaments and ranking and embedded advertising too? (ala Bejeweled or Pogo.com)

I'm talking about taking web technologies far beyond the level of "interaction" that we're accustomed to.

As Douglas Crockford says, "JavaScript is a sloppy language, but inside it there is an elegant, better language." His Little JavaScripter even demonstrates how to rewrite all functions from "The Little Schemer" into JavaScript. Thus, it should be possible to do the inverse (JS->Scheme) given a strict enough subset of JS.

Erlang is a functional language like Scheme, so hopefully JS->Erlang would be similarly possible.

I'm glossing over lots of details of how a "dynamic, run-anywhere, multiuser app" would work, of course. Think of it as an AJAX app with a critical enhancement: write code that runs on the client or on the server, however it suits your problem, with no performance penalty. Make the client as smart or dumb as necessary, and re-balance the client/server workload as you go. Possibly even on-the-fly.

Imagine Google Docs Spreadsheet doing simple calculations on the client, heavy calculations on the server. Maybe you push more calculations to the server when the client has a weak CPU like iPhone.

Ok, this is pretty sci-fi. But tools like Dashcode make AJAX programming awfully accessible. Is the rest so far-fetched?

Last note: Douglas Crockford's JSLint is a JavaScript syntax checker and validator, in JavaScript. His Pratt Parser may be a darn good model to follow.