Content

Not quite a Yegge long.

The odd-looking identity lambda

Tuesday 19 May 2009 - Filed under Code

When I’m not writing something hardcore in C, I’m building tools to support it in C#. That C# is getting increasingly functional and rapidly less imperative, partly because the language is heading slowly in that direction, but mostly because I think they code is much clearer than handrolled loops and mutations and whatever I used to write before discovering these techniques.

There’s some odd things that fall out of this though. In particular, the need to write identity lambdas (a => a) for some surprisingly-common operations.

Here’s a couple of examples. There are many others, but this is what I strike most often:

Suppose we have a list of bools, and we’d like to know if any of them are true:

list.Any(); // incorrect: true if `list` is nonempty.
list.Any( a => a ); // this works, of course.

More useful: we’ve got a list of lists, and we’d like to flatten them down to a single list containing all the elements of the original lists:

list.SelectMany(); // this doesn’t exist. projections 
                   // are assumed to need to *project*

list.SelectMany( a => a ); // oh yeah.

In that second case, it might be clearer to define a function
Flatten :: IEnumerable<IEnumerable<T>> -> IEnumerable<T>
, but this works and is significantly less typing.

Anyway, I think this stuff looks a bit odd, but it’s cool. I have an intuitive sense that a lambda that does nothing is probably bugged, but the cases where it’s legitimately useful are common enough that I might need to adjust my intuition :)

Let me know if there are any other “odd” lambda-related patterns that you’re using in C# – I’ll post them here.

2009-05-19  »  admin

Talkback x 6

  1. Joe Chung
    19 May 2009 @ 1:29 pm

    Your Any example should be list.Any(a => a == true). a => a is Boolean shorthand for a => a == true.

    Your SelectMany example in C# 3.0 parlance can be expressed as:
    from l in list from i in l select i;

  2. admin
    19 May 2009 @ 2:10 pm

    @Joe

    We don’t use the LINQ syntax because the readability and composability is shockingly crap. You run into many of the same walls as when programming in SQL, and those limits are not what our code is about.

    Neither is “expanding” “boolean shorthand”. We don’t do it, and we eradicate whatever unnecessary expansions we find in contributed code. Sorry, but you need to do something about your fear of expressions ;)

  3. Porges
    19 May 2009 @ 3:33 pm

    I would actually just define Flatten as an extension method on IEnumerables. It’s clearer from a code readability perspective.

  4. Sandro
    19 May 2009 @ 4:30 pm

    My Sasa library contains a Flatten extension method on IEnumerable. It comes in handy now and again.

  5. admin
    19 May 2009 @ 5:41 pm

    @Sandro:

    Looks interesting. We also maintain a library for similar purposes.

  6. Isla Watson
    20 May 2010 @ 1:30 pm

    What is the latest and most expensive cellphones this year ?:”:

Share your thoughts

Re: The odd-looking identity lambda







Tags you can use (optional):
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>