Using Lambda for array map() and filter()

Lambda.map()


    // We start by creating an array of integers
    var array = [0, 1, 2, 3, 4]; 

    // We pass the array as the first parameter to the Lambda.map method
    // The second parameter is the map function that we want to use, in this case it returns the array values * 2
    var mappedList = Lambda.map(array, function(v) { return v * 2; } );

    trace(mappedList); 
    // Traces a List object {0, 2, 4, 6, 8}                

Lambda.filter()

    // The filter function returns true if the array value is odd
    var filteredList = Lambda.filter(array, function(v) { return (v % 2 == 0); } );
    trace(filteredList ); // Traces a List object {0, 2, 4}        

Lambda.array()

The Lambda.map() and .filter() methods don't return an array. Instead they return a List object. To get an array, we can wrap the map/filter results into Lambda.array(), like this:

    var array = [0, 1, 2, 3, 4]; 
    var arrayMapped = Lambda.array(Lambda.map(array, function(v) { return v * 2; } ));
    trace(arrayMapped); // Traces a Array object [0, 2, 4, 6, 8]        

Using "using Lambda;"


By adding "using Lambda;" to our class implementation, we get our arrays and lists armed with the Lambda methods:
using Lambda; // <--- 
class Main {
    static function main()     {
        var array = [0, 1, 2, 3, 4]; // 
        var filteredArray = array.filter(function(v) { return (v % 2 == 0); } ).array();
        trace(filteredArray); // Traces a Array object [0, 2, 4]            
    }    
}
version #19937, modified 2014-02-13 23:46:44 by barraponto