elfs: (Default)
[personal profile] elfs
Unless you're a geek, this is going to be meaningless. For months now I've been playing with prototype.js, a Javascript package that does amazing things with packaging and associating code. Many people think that prototype is a kind of miracle. My reaction when first using it was that it must be doing something odd and unusual, exploiting some unintended consequence of the ECMA Javascript standard. I tried to wrap my head around its core concepts and failed. I knew the core concept was in a method called bind, that looks like this:
 function bind(obj, method) {
    return function() { return method.apply(obj, arguments); }
}
While I've been learning about Javascript, one of the things I learned was a closure. A language with closure basically says that:
  • if you declare a function
  • and declare another function within that function
  • and the inner function references variables scoped in the outer function
  • and the outer function returns a reference to the inner function
then, in any call made later to that returned function, the variables scoped to the outer function persist and retain their values at the time the function was returned.

I now understand why progamming is so hard to learn past the basic concepts: instructors are allowed to write the crap like the above when, really, the only way to grok it is to write the code several times until you're comfortable using it. Then you need the vocabulary to explain why it works! In any event, most of the languages I'd worked with up until now didn't have closures; OO was a conceptual addition to languages like Perl and C, and in both languages the addition is simply ugly.

Anyway, an example (in javascript):
 function A(y):
    return function B(x) { return x + y };
}

a = A(7)
a(9) == 16;
a(7) == 14;
etc.. You see how A returns a function (now labeled a), and the value passed into that function is retained by the B function as it's used? Kinda cool: a cheap way to get object-oriented code.

But prototype.js isn't just "object oriented lite". It's a full-blown OO layer on top of Javascript. And the secret is in bind.

Once you understand closures, go back and look at bind up above. The enlightenment came to me this morning. It was a painful, "Duh, oh frack, that's how it works, man am I stupid" kind of enlightenment. bind just says, "There's this thing, and this function associated with the thing, and whenever you call the function you want that particular thing, the one given to you just now, to be a thing you know about." Then I had a grin because I had the most clear realization: Prototype.js isn't doing anything with the language that the language wasn't intended to do. It is, in fact, the sort of thing the ECMA standards writers thought was possible and expected people to do with the language in the first place.

The more I learn about the ECMA standard for Javascript, the more I like the language. It's a full-blown scripting language, and it's typing has the same hijinks as Perl or Python, but the inner syntax has a purity and elegance that makes it much more admirable than the language from which marketers stole its name, hoping to give it cachet.

I don't mention my current language of choice, Python, in the list of languages I'd used that didn't have closures: because it does! I tried it out and was even more delighted to learn that Python has first class closures just like Javascript.

Date: 2006-10-08 04:44 pm (UTC)
From: [identity profile] ibsulon.livejournal.com
Now... for extra fun, go back to your patterns book (I presume you at least have the Gang of Four's book on the shelf - most OO programmers do) and go look at the visitor pattern. NOW, think of this in terms of closures. Next, look at the Strategy pattern, and it's an entire hackaround for a language that doesn't have closures.

Then, check this out. http://www.svendtofte.com/code/curried_javascript/ -- and behold the wonder of the functional language. It took me a while to wrap my head around closures until I realized that I had already done this before in my comparitive languages class in college. :) (Now, to be fair, curried functions are even more powerful, but do check out the article on how to bring the functionality to javascript yourself.)

It's amazing - the more I program, the more I dislike OOP and the more I just wish we could figure out an elegant way to tie databases to functional programming.

Date: 2006-10-08 05:15 pm (UTC)
From: [identity profile] elfs.livejournal.com
By great Cthulhu and all his incomprehensible minions, that has to be just about the coolest thing I've seen all week. It reads a lot like a language formalization of continuations, but with the discipline needed to make them work without too much effort on the programmer's part.

I'd been exposed to currying before but hadn't gotten it then. Part of the problem seems to be that the formal definition, written in Z notation apparently, reads nothing like the informal definition "If you call a function with some required arguments fixed, you get a new function that accepts the remainder of the required arguments." Now that I've had that explained to me, it's much clearer. Thanks!

Actually,

Date: 2006-10-08 06:25 pm (UTC)
From: [identity profile] norikos-author.livejournal.com
Perl _does_ have closures. Functions are first-class entities in perl, and can be passed around and used like any other type of variable.

I cannot recommend _Higher Order Perl_ by Mark Jason Dominus strongly enough. Perl is _far_ more flexible and powerful than I'd realized before I read HOP, and the techniques have helped me with programming in general.

Date: 2006-10-08 07:28 pm (UTC)
From: [identity profile] kyriani.livejournal.com
Javascript has got to be one of the most obtuse but really fun and powerful languages I use (weird how a scripting language that is just interpreted can work like js does). We did a lot of stuff like the package you describe above in our WYSIWYG client website builder tool at my last job, some very AJAX-like stuff before AJAX was even being talked about. We had a javascript (& DHTML) guru though, and he was always willing to help others see what javascript was capable of. Some neat stuff!

Date: 2006-10-08 08:45 pm (UTC)
From: [identity profile] en-ki.livejournal.com
Mmm, curry.

Having learned to program elsewhere, I really envy MIT students their early exposure to Scheme.

Date: 2006-10-08 10:15 pm (UTC)
From: [identity profile] atheorist.livejournal.com
If you like currying and closures and simplicity, you would probably like lambda calculus.

You might enjoy reading about object encodings in lambda calculus - Benjamin Pierce has a book called "Types and Programming Languages" which explains some of the cool things you can do.

Date: 2006-10-08 11:45 pm (UTC)
From: [identity profile] tamino.livejournal.com
I use closures in python all the time. I also love generators. Two great ideas that both came from lisp :-)

Profile

elfs: (Default)
Elf Sternberg

May 2025

S M T W T F S
    123
45678910
111213141516 17
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 30th, 2025 11:38 am
Powered by Dreamwidth Studios