Javascript Jems - Lambda expressions
Thursday, 08 July 2010 00:00
Article Index
Javascript Jems - Lambda expressions
Using anonymous functions and invocation

Banner

There is a lot of current talk about Lambda expressions, so much that it's difficult to know what they really are all about but, guess what, Javascript does Lambda expressions out of the box.

First what is a Lambda expression?

Lambda expressions were invented by Church and Kleene, two great computer scientists, back in the 1930s. In practice this is fairly irrelevant in the sense that when most programmers use the term Lambda expression they mean a function of a number of parameters that returns a single result and can be passed to other functions.

The parameters and single result clearly aren’t a problem but what about passing a function to other functions?

This sounds more complicated but it's no problem in Javascript.

Javascript is an interpreted language and this means that it treats code and data much the same.

Functions as data

What this means in practice is that a variable can store data or code with the only real difference being that code can be executed using, for example the invocation operator ().

The usual way to define a Javascript function covers up this unity.

For example, the definition:

function test(a)
{
alert(a);
}

look like a standard function creation however it’s just shorthand for the assignment:

test=function(a){alert(a);}

You really can think of test as being a variable that just happens to hold a function.

Notice that you can assign one function to another using standard syntax:

var test2=test; 
test2("hello2");

and this works perfectly.

Notice that while test and test2 are both references to the code that make up the function they  behave like a simple reference would.

In particular if you redefine test a new block of code is created on the heap and test is set to point to it. The second variable, test2 still points to the original block of code and hence the old version of the function.

For example following:

test=function(){alert("Hello1");};
test2=test;
test=function(){alert("Hello2");};

test is a function that prints Hello2 but test2 is still the original function that prints Hello1, as you can confirm by using:

test();
test2();

In this sense Javascript functions are immutable – you can’t modify them only create completely new functions.

You can use this function reference mechanism to create something that looks like C#’s multi-target delegate function.

To call multiple functions with a single call you could use something like:

func1=function(){list of instructions 1};
func2=function(){list of instructions 2};
func3=function(){list of instructions 3};
func={func1();func2();func3()};

and to call all three functions one after another you would use:

func();

You can pass parameters if you want to and pass them on to the sequence of called functions. This is all particularly useful if you want to implement a “callback” that invokes multiple functions.

Passing functions

Given you can pass a variable, i.e. as a parameter, into functions it now seems entirely reasonable that  you can pass a variable that just happens to contain a function into another function. Recall that all parameters in Javascript are passed by reference so just a pointer to the function is passed and this is efficient.

For example, the Array object has a method that will sort the array into order:

var list=new Array("A","AB","ABC","ABCD");
list.sort();
for(var i=0;i<list.length  ;i++)
{
alert(list[i]);
}

What is perhaps less well known is that the sort method can also take an optional function which it will use to compare the values of the array. This function is defined as:

function(a,b)

and it has to return a negative value if a<b, 0 if a==b and a positive value if a>b.

For example:

compare=function(a,b)
{
return a.length-b.length;
}

used in

list.sort(compare);

sorts the list of strings into order based on their length alone.

Banner

<ASIN:0596517742>

<ASIN:0321572602>

<ASIN:0596806752>

<ASIN:1590597273>

<ASIN:059680279X>



Last Updated ( Monday, 09 August 2010 16:38 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2013 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.