John Lindal’s Blog
JavaScript Scoping
August 16, 2010 on 1:36 pm | In Programming | No CommentsI’ve been working my way through Douglas Crockford‘s video series on JavaScript. This morning, while waking up, I found myself wondering how to implement closures with static scoping.
As background, Brendan Eich was forced to create JavaScript in a matter of weeks, so we should all be grateful that he didn’t implement dynamic scoping. This is the simplest to build, since the rule is If the variable wasn’t defined in this stack frame, search backwards through all the stack frames to find the requested name.
Unfortunately, it’s a nightmare to work with, since it’s very difficult to predict what the stack will contain when your function is called. Language designers have learned this the hard way. If I remember correctly, Lisp and Perl both started with dynamic scoping.
So JavaScript implements static scoping. A scope is created every time a function is defined — not every time an open curly brace is encountered. This allows you to look at your source code and know what will be available in the closure: the variables defined in the function, the variables defined in the enclosing function, etc. — all the way up to global scope.
What is the simplest implementation of static scoping?
Start with a stack frame (hash map) at global scope. When the execution reaches a statement that defines a new function (scope), then save a reference to the current stack frame as the function’s enclosing scope. When a function is executed, store a reference to its enclosing scope in the stack frame. If the function defines another function, then this new function will save a reference to the current stack frame, which has a reference to its enclosing scope, etc. — all the way up to global scope. With garbage collection, stack frames which are referenced by function instances or other stack frames will not be lost when the executing function exits.
The Sword of Good?
May 31, 2010 on 3:12 pm | In Miscellaneous | No CommentsUpon finishing Eliezer Yudkowsky’s The Sword of Good, it weighed heavily on my mind. A couple of hours later, I finally connected all the dots. It was wrong, deeply wrong, but it felt like the author agreed with what he wrote. I set out to analyze my initial thoughts…
First, why is it wrong?
The denouement comes when Hirou (surely a play on Hero), the bearer of the Sword of Good, sides with the Lord of Dark, enabling him to complete the Spell of Infinite Doom. Hirou chooses to do this because the Lord of Dark intends to end all the suffering in the world — and there is much, make no mistake! Noble though it seems, it is in fact Evil because it abolishes free will and turns everybody into a puppet:
…the great Balance of Nature will be, not upset, but annihilated utterly; and in it, set in place a single will, the will of the Lord of Dark. And he shall rule, not only the people, but the very fabric of the World itself, until the end of days.
Without free will, everybody might as well be dead. If sentience is lost, then they really are dead. If sentience remains, each person is imprisoned in their own head, unable to do anything but watch while the Lord of Dark imposes his will upon the world.
Second, why do I worry that the author agrees with Hirou’s decision?
In 2004, the author’s brother died, and the author announced this via an email. To me, the author’s rage against the universe in general and death in particular in this email sounds very similar to the Lord of Dark’s rage against Balance and the flood of pain with which the Sword of Good deluges Hirou during the story’s climax. Perhaps reconciliation for the author lies in Hirou’s last words:
“I don’t trust you either,” Hirou whispered, “but I don’t expect there’s anyone better”
but this seems pretty weak to me. What need is there for trust when everybody is for all intents and purposes dead?
Perhaps the story is instead intended as a cautionary tale, exemplifying the maxim, The road to Hell is paved with good intentions.
This seems plausible, because after touching the Sword of Good, the Lord of Dark is warned by Hirou that The Sword only tests good intentions.
Hirou clearly made his decision under duress: the Sword of Good dumped all the suffering in the world on him in one instant. If we interpret the conclusion as a warning to not press the Big Red Button when you’re in the grip of strong emotions, then it feels right.
It’s a Small World
March 20, 2010 on 8:41 pm | In Computers, Sightings | No CommentsMy wife just took a writing course from Bonnie MacBird, the original author of TRON and the wife of Alan Kay, one of the inventors of the graphical user interface at Xerox Parc.
The iPhone Unlocked
March 15, 2010 on 1:23 pm | In Miscellaneous | No CommentsPeople despair that the iPhone remains a closed platform, but I don’t think this will last forever.
When the iPhone was first released, Apple claimed it didn’t need applications — web apps were the way to go — but once Apple extended the iTunes store, they went app-crazy. Multitasking has been ruled out since the iPhone’s inception for several good reasons, including battery life, but that is apparently about to change, too — reasons be damned. Apple is building a history of changing their minds. Big surprise
They do whatever they believe will make them the most money at the time.
I don’t know of any serious competitors to iTunes, and from what I hear, all the up-and-coming competitors to the iPhone are currently unpolished enough that ordinary people still prefer the iPhone. Apple has a monopoly and is milking it as best they can. Despite the example set by Windows — it has never managed to compete with Mac OS on polish — I hope that other handsets will eventually provide serious competition for the iPhone. Windows is the 800 pound gorilla, so it never had to care, but the phone market is much more competitive. When the competition catches up, Apple will no longer have a monopoly, and they will have no choice but to stop acting that way.
The same holds true for the iPad. Apple originally laughed at the netbook market. Now they have a netbook killer — at least for ordinary people. Some iPhone apps will be usable on the iPad, especially games like Canabalt and Evacuation, but most apps will need to be re-imagined for the larger screen. This monopoly is barely getting started. We can only hope that competitors to the iPad will eventually force Apple to unlock it, too.
The Arrow of Time
March 2, 2010 on 8:34 pm | In Deep Thoughts, Math / Physics | No CommentsI just read an article about the Arrow of Time. The foundation of the author’s discussion was entropy and the observation that it always increases. From a macroscopic perspective, this is reasonable, but it seems to me that there is an even deeper argument. If I understand the Free Will Theorem correctly (always iffy for deep stuff like that), then, assuming humans have free will, the result of measuring properties at the quantum level cannot be known ahead of time. I believe Conway stated that the result doesn’t exist until the experiment is performed. I can’t conceive of anything more unidirectional than that. There is just no going backwards.
Note that the Free Will Theorem directly contradicts the article’s claim that knowing the exact state of the universe allows you to compute the future.
The Matrix
February 13, 2010 on 1:47 pm | In Deep Thoughts, Movies | No CommentsWhy is it that people so dislike the sequels to The Matrix? Is it just because they contained so much more dialogue? Or is it that the battle against the system, so passionate in the first movie, was tempered by the realization that even resistance is just another cog in the system?
New MacBook Pro
February 9, 2010 on 7:53 pm | In Computers, Deep Thoughts | No CommentsThis is my first blog post from my brand new, shiny unibody MacBook Pro. I like the no-hook lid. I love the trackpad without any buttons. When I was born, the mouse had barely been invented, and now my daughter isn’t yet five years old, and she’s already growing up in a world where the mouse is obsolete.
Update (2/21/10): Trackpads with separate buttons now feel really strange. And I love how far back the display tilts — much further than any other laptop I’ve ever used, and so much more comfortable!
Lisp
January 21, 2010 on 11:45 am | In Programming | No CommentsWhile driving to work this morning, I finally separated out what has been bothering me about Lisp. Functional programming proponents tend to make a big deal about avoiding side effects because it avoids long range coupling between functions and it makes list iteration trivially parallelizable. This is good for program maintenance and effective utilization of all the cores in a CPU.
Side effects are eliminated by avoiding (1) global variables and (2) functions that modify their arguments.
Point #1 is relatively easy in any language (except assembly), but it does require discipline, since global resources like files and databases are in principle always directly accessible, even if an encapsulating interface exists.
Point #2 is very difficult/painful in strongly typed languages like C++ and Java but very easy in untyped languages like Lisp, Perl, and JavaScript, mainly because untyped languages make it very easy to return a heterogeneous list of values/objects.
This is what has been bothering me about Lisp in the back of my mind: a list is the most natural return value, both because the syntax is simple and because most standard functions operate on lists. A homogeneous list is fine, of course, but a list of heterogenous values is terrible because (1) it is hard to remember what is returned in each slot and (2) without compile time checks, modifying a function to insert a new value into the returned list creates a maintenance nightmare. You have to manually find and update every use of the modified function, and if you miss one, you have a subtle bug.
How can we avoid all this trouble? Using a map instead of a list alleviates the problems because (1) well chosen key names are easier to remember and (2) most of the existing uses of the function probably will not need to be updated because they do not care about the new value and the original values will still be accessible via the same keys.
Unfortunately, working with maps (or hash tables) in Lisp is a lot messier than working with lists. In Perl and JavaScript maps are part of the language syntax.
The Future of UI is not boring
January 15, 2010 on 11:27 pm | In Computers, Deep Thoughts | No CommentsScott Berkun’s tangent off John Gruber’s wistful piece about the Apple Newton is flat out wrong. The future of UI is here already, and it is anything but boring. 10/GUI was announced several months ago, and SixthSense has been under development for a while. Both have the potential to revolutionize how we use computing power. The former replaces the ubiquitous desktop mouse, while the latter makes the iPhone seem antiquated and clunky. While neither will be available by Christmas 2010, gestures are invading our computing experience. Apple continues to execute on its long-term plan by introducing more and more gesture recognition in every new model of laptop and mouse. Designers who are not already thinking about how to effectively use the expressive power of systems like 10/GUI and SixthSense will probably find themselves out of work by 2020.
Avatar
January 13, 2010 on 11:17 am | In Miscellaneous | No CommentsThe game has changed.
I was very skeptical when I read that James Cameron was out to out-Lucas George Lucas. I don’t believe it’s possible, but only because every game changing quantum leap up has the same general effect of blowing the public’s collective mind. But there is no question that Avatar has now changed the game in exactly the same way that Star Wars did thirty years ago. Audiences will not put up with 2D summer blockbusters much longer.
The story is certainly not as sweeping in scope as Star Wars, but Cameron deserves credit for at least thinking up an entire continent instead of only a few square miles of jungle. The characters were all stereotypes, and they died in the standard order, but they were still well done and served the purpose of making as deep an emotional impact as possible. As far as I can tell, that was what Cameron wanted: amplify the impact of watching full 3D by giving the audience characters to whom they can readily respond.
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^