savemytext is a tiny app engine webapp I wrote last night.
When you write text in it, it will save your text. When you come back, your text will still be there. You can have multiple text boxes. Since it’s an appengine app, you’ll need a google account to use it.
A trivial app, to be sure. But all too often I find myself writing gmail drafts just in order to get a persistent text box between computers. I figured, why not do it properly?
(I was going to just call it “textarea”, but someone already stole that name (and tens of similar ones, without actually having an app to put up. How rude))
I’m pleased to report that my crazy notions of replacing javascript metaprogramming with lisp metaprogramming appear to be headed steadily in the direction of success. It’d take a while to explain exactly what I’m up to (and the reasoning behind it), but essentially I’m trying to solve the same problem that async.js, narrative javascript and strands all try to solve: asynchronous callbacks are ugly, error-prone and downright confusing.
All of the above mentioned tools are unsatisfactory in various ways. Both narrative javascript and strands are complex enough that I uncovered serious bugs in both reasonably quickly. async.js is much more stable (and I actually have a working application with it), but still requires very careful programming and only works on mozilla browsers.
So I set forth with parenscript, which is essentially javascript dressed up as lisp. It doesn’t make for any prettier javascript, but it does make for one hell of a metaprogramming opportunity using lisp macros.
The aim is to convert straightforward, procedural-style code into the contortion required to appease asynchronous callbacks. I’m certainly not done yet, but I do have some compelling proof to show that it’s a plausible thing to do with lisp. Here’s a contrived example for, say, getting all items from whatever feed the supplied item-id belongs to. This involves getting the item to find the feed it belongs to, then returning all items contained within that feed. The “store” objects in this scenario refer to lawnchair stores, which do asynchronous local datastore lookups:
(asyncfunget-sibling-items(item-id)(deferitem(item-store.getid))(console.log(+"item belongs to feed: "item.feed-id))(deferfeed(feed-store.getitem.feed-id))(retfeed.all-items))
And here is the generated javascript:
functiongetSiblingItems(itemId,cb){itemStore.get (id,function (item){console.log('item belongs to feed: '+item.feedId);feedStore.get(item.feedId,function (feed){cb(feed.allItems);});});};
This is just a simple example, but the lisp code still shows a remarkable reduction of both noise and contorted control-flow, and the generated code ought to work in all browsers. Worth pursuing, certainly.
So it’s not something you should have to worry about (often), but I just spent an inordinate amount of time figuring out exactly why a particularly innocuous-looking piece of code was failing. It’s also yet another surprising part of the javascript language, and if you often deal with javascript then it’s in your best interests to keep track of such oddities (there are a lot of them!).
To illustrate, consider the following array intersection function:
varlist1=["a","b","c"];varlist2=["a","b","d"];functionintersect(a,b){// find the intersection of two arraysvarintersection=[];jQuery.each(a,function(){if(b.indexOf(this)!=-1){intersection.push(this);}}returnintersection;}
You might expect calling intersect(list1, list2) to return ["a", "b"], since those are the common elements in the two sets. However, what you really get is [].
The problem is that I was a bit lazy, and used jQuery.each() to iterate over my collection (I hate the for x in y construct, and for loops are just so C). The each method calls your provided function on each member of the provided array, setting this in the context of each call to the current array element.
But it turns out that when you call a method on a primitive string object, the javascript (well, ECMAScript really) language specifies that this call takes place not on the string primitive type, but on the String object type (it’s kinda like a boxed value in Java). That is, fun.call("some string") actually ends up as if you had written fun.call(new String("some string")).
The String and string objects will compare equal when using the “==” operator - but since they are of different types, they will not compare as equal using the “===” operator. Evidently that is the type of equality that indexOf() uses, therefore none of the String objects will ever appear in the array of string primitives.
Normally, you will neither notice nor care. However, when extending the String class or calling a function with a primitive type as the subject, you should keep this conversion in mind. Note that the conversion does not apply to function arguments, only to the subject of a function application (i.e this).
I’m continually amazed by the high quality answers found on stackoverflow.com - I posted this question asking why the object promotion occurs (and if that is indeed what’s going on), and was rewarded with an explanation and a link to the precise part in the ECMAScript spec where the behaviour is detailed.
I’ve recently started using bluetile instead of compiz, and I think I’m going to stick with it. Bluetile is a tiling window manager, but it’s not like all those other tiling window managers*:
it’s simple
it’s easy to learn
it’s built with GNOME support out of the box
it still supports direct manipulation style using the mouse if you just want to change a window’s size, or swap window locations. I think this is crucial for keeping it accessible to new people like myself.
* well okay, it is like those other tiling window managers. Specifically, it’s built on xmonad. But it presents a simple veneer over the vast complication that is xmonad (I should know, I tried to configure it just last week ;)).
Bluetile is, sadly, notoriously difficult to install. But there are good guides around, I used this one for ubuntu karmic.
The good news is, bluetile has recently been merged back into the xmonad mainline. And xmonad is apt-gettable (at least on ubuntu). So in theory, the next release of xmonad will make it pretty easy to run in bluetile mode, without having to compile-your-own-window-manager (which certainly does not make for a gentle learning curve). If anyone is considering playing around with alternate window managers, it’s definitely worth your while to give bluetile a go.
async.js is a fascinating library for taming asynchronous javascript. It’s highly experimental, and as far as I know, it only works in firefox. But the idea is an important (and useful) one, so I think it’s definitely worth knowing about.
There are a few approaches to dealing with asynchronous javascript:
Compile [some language] to javascript
This is the approach taken by GWT, pyjamas, and many others. It’s usually extremely heavyweight, so it mainly makes sense for big apps.
Compile [almost-javascript] to javascript
Most notably this includes Narrative Javascript and its successor, Strands. This is a reasonable approach, but the lack of maturity / tool support makes debugging extremely hard. Also, I ran into a number of bugs in both these libraries. The thought of finding and fixing more of those bugs is not at all fun.
Write a javascript library
This is doable, but typically looks hideous, convoluted, and is usually quite burdensome to try and use.
Async.js is the most plausible attempt at #3 that I’ve seen. It uses a reasonably clever (but not unknown) trick to to turn Javascript 1.7’s generator functionality into an event-based coroutine system. Specifically, this allows for a program to “wait” for a callback, while not actually blocking the javascript interpreter (as a synchronous AJAX call would). Go read the async.js page to learn more, because the following isn’t going to make much sense if you don’t know roughly how to use async.js.