Functional programming with Dojo – Quick tips

This post is a summary, and more, of a great blog post by Eugene Lazutkin on the insanely cool new Functional Programming features in the Dojo Ajax toolkit. It gives number of quick examples in the vein of : “How to I do (x) to a data structure of type (y)“. Have a read of Eugene’s post if you want to know the reasoning behind these features, and how they can be used to vastly simplify your own code.

Please feed free to post your own suggestions as comments and I will include them here. Note that some of the examples contain the variable df, this is short for dojox.lang.functional.

1. How do I add a number to every element of an array (creating a new array)?

var list= [10,20,30,50,25,5];
var newList= dojo.map(arr, "return arguments[0]+2");

2. How do I add a number to every element of an array without creating a new array?

var list= [10,20,30,50,25,5];
dojo.forEach(list, "array[index]+=2");

3. How do I sum all the values in an array?

var arr = [10,20,30,50,25,5];
var sum = df.reduce(arr, "+");

4. How do I sum all the values in a JSON array containing objects?

var json = [{value: 1, name:"foo"}, {value:25}, {value:12,name:"bar"}];
var sum = df.reduce(json, "{value:a.value+b.value}").value;

5. How do I check if a condition is true for all elements of an array?

var arr = [10,20,30,51,25,5];
var isTrue = dojo.every(arr, "return item % 5==0;");

6. How do I check if a condition is true for at least one element of an array?

var arr = [10,20,30,51,25,5];
var isTrue = dojo.some(arr, "return item % 5==0;");

7. How do I find the smallest element in an array?

var arr = [10,20,30,51,25,5];
var min = df.reduce(arr, "Math.min(a,b)");

8.How do I find the largest element in an array?

var arr = [10,20,30,51,25,5];
var min = df.reduce(arr, "Math.max(a,b)");

9. How to I calculate the percentage each element of an array makes up of the sum of all elements in the array?

var arr = [10,20,30,51,25,5];
var percents = df.map(arr, "/this", df.reduce(arr, "+"));

10. How do I generate a running total of an array?

var arr = [1,2,3,4,5];
var runningTotal = df.scanl1(arr, "+");
//Gives runningTotal=[1,3,6,10,15]

11. How do I create an array where each element is the multiplicand of all elements that came before it and itself?

var arr = [1,2,3,4,5];
var result = df.scanl1(arr, "*");
//Gives result=[1,2,6,24,120]

12. How do I combine multiple arrays into an equal number of arrays, but arranged by index?

var arraysByIndex = df.zip([1,2,3],[11,22,33],[111,222,333]);
//Gives arraysByIndex=[[1,11,111] , [2,22,222], [3,33,333]]

13. How do I search a dojo.data store for the existance of a named attribute?
//Assume a data store exists called 'store' and items have been retrieved
//and placed in an array called 'items'. We will search for an item that has
//an attribute 'country' equal to 'Ireland'
var found = df.some(items, "this.getValue(item, 'country') == 'Ireland'", store);

14. How do I filter an array to just the items that match a boolean condition?
var array = [22,4,66,100,55];
var over30 = dojo.filter(array, "return item > 30");
//Gives a new array [66,100,55]. The original array is not modified.

15. How do I find an element in an array?

var array = ["this","is","an","array","of","strings"];
var index = dojo.indexOf(array, "an");
//returns 2

16. How do I apply a Django Template to one or more nodes?

dojo.require("dojox.dtl.ext-dojo.NodeList");
var array = ["some", "sample", "template", "input"];
dojo.query(".someClass").dtl("templates/myTemplate.html", array);

17. How do I remove elements from an array without creating a new array?

var arr = [1,2,3,4,5,6,7,8,9,10];
// Removes all even numbers from the array
dojo.forEach(arr, "if(array[index] % 2 == 0){array.splice(index, 1);}");
// arr now contains [1,3,5,7,9]

Share this post:digg it|kick it|Email it|bookmark it|reddit|liveIt

11 thoughts on “Functional programming with Dojo – Quick tips

  1. Is it possible to use any of the dojo iterators to modify the array they are interating over?

    I need to remove elements from an array that meet some condition, each element also contain reference to another array.
    So I do not want to make duplicate of it.

    Thank you

  2. How could I get the index of an item in an array without combining indexOf and filter ? πŸ™‚

    dojo.myIndexOf(arr, function(item, i) { return (item > 0) }) ~==
    dojo.indexOf(arr, dojo.filter(arr, function(item, i) { return (item > 0) }))

  3. @Claudio,

    I’m not sure what you are trying to achieve. For simple searching, i.e. numbers or strings, dojo.indexOf finds the correct index.

    If you want to find the index of the first element that matches a given condition, you could use dojo.some, e.g.

    var idx;
    dojo.some(arr, function(item, index){
    if (item > 0) {
    idx = index;
    return true;
    }
    return false;
    }

    Shane

  4. Building off of what Claudio asked, lets say i have an array of elements (which may not have unique content in the innerHTML), and one of those elements fires an event. Can i easily find the index of the element that fired the event?

Leave a reply to Claudio Cancel reply