Functional programming

Friday 23rd of November 2012 09:16:42 AM


  Toggle Advanced Options



Functional JavaScript programming

Fundamental functional programming functions (for collections):

function forEach(array, func) {
    for (i = 0, len = array.length; i < len; i += 1) {
        func(array[i]);
    }
}

function reduce(combine, base, array) {
    forEach(array, function (item) {
        base = combine(base, item);
    });
    return base;
}

function map(func, array) {
    var result = [];
    forEach(array, function (element) {
        result.push(func(element));
    });
    return result;
}






Google