Using Lambda .map() and .filter()

The Haxe Array class doesn't have map() or filter() methods, which can be a bit confusing for people coming from other programming languages. Instead we find them as members of the Lambda class. It may seem a bit strange and verbose at first, but hang on - when just have to add the "using" directive to get the handy functions at our fingertips.

Lambda.map() and Lambda.filter()

The map and filter function live as static methods in the Lambda class. They can be used like this:

    // 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.filter 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}                

Here's an Lambda.filter() example in the same manner:

    // 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. (Note the curly braces in the trace outputs above!) 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]        

Note that the trace output now is wrapped in square brackets, which means that it's an array, not a list any more.

Using "using Lambda;"

To get a an elegant and non-verbose version of the above examples, we just add Lambda to our array/list tool arsenal by adding it as a "using" directive. Adding "using Lambda" to the class implementation gives us direct access to Lambda methods for our lists and arrays:

using Lambda; // <--- 
class Main {
    static function main()     {
        var array = [0, 1, 2, 3, 4]; // 
        // Now we can use the filter() method directly on our array:
        var filteredList = array.filter(function(v) { return (v % 2 == 0); } );
        trace(filteredList); // Traces a List object {0, 2, 4}        
    }    
}

The filter() function returns a List, which means that the return value itself is armed with Lambda functions. So let's add .array() to the result:

    var array = [0, 1, 2, 3, 4]; // 
    var filteredList = array.filter(function(v) { return (v % 2 == 0); } );
    trace(filteredList.array()); // Traces an Array object [0, 2, 4]        

As an example of code minimalism, we can chain both functions to the original array itself:

    var filteredArray = [0, 1, 2, 3, 4].filter(function(v) { return (v % 2 == 0); } ).array();

version #15776, modified 2012-12-09 13:45:29 by cambiata