Links

Categories

Tags


« | Main | »

Lambda expressions

By Jewe | June 4, 2014

// define a condition delegate type
delegate bool Condition(const string);

class DataSet
{
    string[] Data;

    method DataSet(const string[] data)
    {
        Data = data;
    }

    // returns elements that match the given condition
    method string[] Select(Condition c)
    {
        string[] result;
        for (int i = 0; i < Data.length; i++)
        {
            if( c(Data[i]) )
                result += Data[i];
        }
        return result;
    }
}

function string main(const string[] args)
{
    // create a data-set of names
    DataSet names = new DataSet({ 
        "Judy", "Christopher", "Helen", "James", "Sandra", "Rick", "Elisabeth" });

    // Select all names that start with "J"
    string[] namesWithJ = names.Select( (i) => i.startsWith("J") );

    return null;
}

Topics: code examples | Comments Off on Lambda expressions

Comments are closed.