Lambda Expressions - I’m in love!

Probably the most geeky title ever written! But hey – they are sooo bad ass! Seriously! Think of this post as a history lesson or nostalgic trip if you like!

(Meanwhile, yes, my feed is down. Starting to regret my moving feedburner to feedburner.google. The big search company is just another company. Wonder when the public will realize? It’ll be interesting to see if I have any readers left at all once google gets their act together!)

Without lambdas – the classic old school way:

public class Program
{
    public void Main(string[] args)
    {
        Classic();
        ClassicTwo();
    }

    public void Classic()
    {
        ALittleObject aLittleObject = new ALittleObject();
        aLittleObject.Value = "foo";
        aLittleObject.Key = "42";

        aLittleObject.Displayer = new DisplayA();

        Console.Write("foo" == aLittleObject.DoDisplay);
    }

    public void ClassicTwo()
    {
        ALittleObject aLittleObject = new ALittleObject();
        aLittleObject.Value = "foo";
        aLittleObject.Key = "42";

        aLittleObject.Displayer = new AnotherDisplayA();

        Console.Write ("foo (42)" == aLittleObject.DoDisplay);
    }
}
public class ALittleObject
{
    public string Key { get; set; }
    public string Value { get; set; }

    public IDisplayA Displayer { set; private get; }

    public string DoDisplay
    {
        get { return Displayer.DoDisplay(this); }
    }
}

public interface IDisplayA
{
    string DoDisplay(ALittleObject aLittleObject);
}

class DisplayA : IDisplayA
{
    public string DoDisplay(ALittleObject aLittleObject)
    {
        return aLittleObject.Value;
    }
}

class AnotherDisplayA : IDisplayA
{
    public string DoDisplay(ALittleObject aLittleObject)
    {
        return aLittleObject.Value + " (" + aLittleObject.Key + ")";
    }
}

Same thing but with delegate and now tests – somewhat better:

[TestFixture]
public class Tests
{
    [Test]
    public void SlightlyBetterWithOwnDelegate()
    {
        BLittleObject bLittleObject = new BLittleObject();
        bLittleObject.Value = "bar";
        bLittleObject.Key = "42½";

        bLittleObject.Displayer = delegate(BLittleObject bLittleO)
        {
            return new StringBuilder().AppendFormat("{0} ({1})", bLittleO.Value, bLittleO.Key).ToString();
        };

        Assert.AreEqual("bar (42½)", bLittleObject.DoDisplay);
    }
}
public class BLittleObject
{
    public string Key { get; set; }
    public string Value { get; set; }

    public DisplayB Displayer { set; private get; }

    public string DoDisplay
    {
        get { return Displayer(this); }
    }
}

public delegate string DisplayB(BLittleObject bLittleObject);

With lambdas – what’s not to love?

[TestFixture]
public class Tests
{
    [Test]
    public void SystemDelegateWithLambda()
    {
        CLittleObject cLittleObject = new CLittleObject();
        cLittleObject.Value = "baz";
        cLittleObject.Key = "42:ish";

        cLittleObject.Displayer = cli => string.Format("{0} ({1})", cli.Value, cli.Key);

        Assert.AreEqual("baz (42:ish)", cLittleObject.DoDisplay);
    }
}
public class CLittleObject
{
    public string Key { get; set; }
    public string Value { get; set; }

    public Func<CLittleObject, string> Displayer { set; private get; }

    public string DoDisplay
    {
        get { return Displayer(this); }
    }
}

Cheers,

M.

Technorati Tags: ,

posted @ Friday, October 17, 2008 11:27 PM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 1 and type the answer here:
 

Live Comment Preview: