Random thoughts from a Firefox

Robert O'Callahan has a nice recap of BaaCamp down under. Robert talks about various talks that he went too, for example on AIR which touted: features that can and should be provided by browsers too, possibly via extensions like Gears for the lag...

Functional Programming with JavaScript and Dojo Print E-mail
User Rating: / 0

Eugene Lazutkin has written a piece on Functional fun in JavaScript with Dojo where he delves into the land of functional and how it is available in JavaScript.

Eugene maps out some of the helpful functions that JavaScript itself has added over time:

  • JS 1.6 (in Firefox 1.5) introduced
    so-called Array extras:
    special Array methods, which help to simulate lists with arrays:
    indexOf(), lastIndexOf(), every(), some(), filter(), map(),
    forEach(). The last five methods are especially important because
    they help to eliminate the most common direct loops.
  • JS 1.7 (in Firefox 2) introduced Array comprehensions borrowed from Python. The new syntax allows to generate arrays using
    a compact yet clear notation reducing the possibility of errors. And
    of course iterators and generators will helps us with cleaner loops
    too. Another goody is the block scope with ?let?.
  • JS 1.8 (in Firefox 3) brought us
    more Array extras:
    reduce() and reduceRight(). They give us a native support for
    all-important folds. Another notable additions are expression
    closures (simplified one-line functions), and generator expressions.
  • JS 2 (ES4 PDF)
    takes us even farther: for each statement, tail calls, and the whole
    raft of language improvements. Presumably JS 2 will come with the
    next generation of JavaScript virtual machines helping to reduce
    penalties for using new abstractions.

... and how Dojo implements many so you have cross browser access.

He goes into detail on his favourite five: filter(), map(), forEach(), every(), and some().


var percents = dojo.map(values, function(val){ return val / sum; });

var sum = 0;
dojo.forEach(values, function(val){ sum += val; });

Next he goes beyond core to dojox.lang.functional where lambda is your friend:

var div2 = df.lambda("/2");

What about performance? We get a nice run down on the performance of the Dojo functions compared to native ones if they are available.

Very thorough indeed.

Read more at: http://feeds.feedburner.com/~r/ajaxian/~3/228887277/functional-programming-with-javascript-and-dojo.
< Prev   Next >