02
Mar
09

first class functions in programming languages



I first learned about first class functions in Javascript, and I really loved the possibility of doing something like this :


function x(a,fun) {
  for(var i = 0;i<a.length;i++) {
    fun(a[i]);
  }
}

x([1,2,3],function(x) {
    alert(x);
});

The possibility of passing whatever variable and using it as a function blew my mind 3 years ago. These days I use Javascript very little, I’m doing most of my coding in Java/Python/Ruby. We all know the deal about Java and first class functions ( sucks ). Ruby has lambdas and procs, which, in order to be executed, you must call the call method… so, there’s an extra step you have to make. The closest to Javascript syntax is Python, where you have a function like this :


def x(a,fun):
  for elem in a:
    fun(a)

def show(elem):
  print elem

# and you call it like this:
x([1,2,3],show)

Can your language do this? 🙂

Advertisements

1 Response to “first class functions in programming languages”



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


Blog Stats

  • 226,733 hits

%d bloggers like this: