Dynamic Languages

In the last few days I have started looking into dynamic computer languages such as Ruby, JavaScript (I’ve done JavaScript before, but never took the time to “learn” it), and Groovy.  The one thing I really like about them so far are closures.  The idea of encapsulating code and environment into a variable is so amazing to me.

This little snippet is taken from the mozilla site.


function makeAdder(x) {
 return function(y) {
 return x + y;
 };
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

print(add5(2));  // 7
print(add10(2)); // 12

the variables add5 and add10 become closures for the code and the environment, meaning they retain the local variable values! People are talking about the need of closures in Java, and I agree for no more than they are fun and very convenient. I hope they get them. I am thoroughly enjoying that feature in these dynamic languages. I am looking forward to exploring these more.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s