locked
How do you read a lambda expression? RRS feed

  • Question

  • Hi All,

    I am trying to figure out how to read out loud a lambda expression.  For example if I have the following

    i => (i %2) == 0 or c => c.Age < 50 etc.

    How would I read them to my children?

    Cheers,

    Clint

    Thursday, January 12, 2006 6:57 AM

Answers

  • I think how a lambda expression is read depends on the context in which it is used.  For example,

    In numbers.Where(num => num < 5), the lambda expression is a predicate, so the lambda can naturally be read as "num where num is less than 5".

    In numbers.Select((num, index) => new {Num = num, InPlace = (num == index)}), the lambda expression is a projection selector, so I would read it as "num, index, selecting Num as num, InPlace as num equals index".

    Verbalizing out of context is a little more arcane, but the fact that the lambda expression is really an anonymous method means it makes sense to vocalize the function call and say something like:
    "num, calling num less than 5"
    "num, index, calling new Num equals num, InPlace as num equals index"

    -Mathew

    Saturday, January 14, 2006 1:04 AM

All replies

  • personally, i love the lambda expression additions.

    in school, i used SML most of the time, and it was an amazing language.

    i know lamba expressions are probably a little foreign for a lot of programmers,

    but the power of being able to inline functions creates many possibilities for streamlined implementations.

    give it a chance, and then you probably will wonder where they have been your whole life.

     

    Thursday, January 12, 2006 7:29 AM
  • I would read it like:

    i => (i %2) == 0: returns true when i is even

    c => c.Age < 50 : returns true when c's age is below "way too old"

    Thursday, January 12, 2006 10:20 AM
  • I've always read it as 'in which':

    "c in which Age is less than 50"

    etc.

    Thursday, January 12, 2006 12:49 PM
  • I think they are fantastic as well since they make the code more visually readable but I am still trying to figure out if there is an official way to read it out loud.

    Cheers,

    Clint

    Thursday, January 12, 2006 5:49 PM
  • so maybe read it like

    "i lambda returns true when i mod 2 is equal to 0"?

    Cheers,

    Clint

    Thursday, January 12, 2006 5:52 PM
  • I think how a lambda expression is read depends on the context in which it is used.  For example,

    In numbers.Where(num => num < 5), the lambda expression is a predicate, so the lambda can naturally be read as "num where num is less than 5".

    In numbers.Select((num, index) => new {Num = num, InPlace = (num == index)}), the lambda expression is a projection selector, so I would read it as "num, index, selecting Num as num, InPlace as num equals index".

    Verbalizing out of context is a little more arcane, but the fact that the lambda expression is really an anonymous method means it makes sense to vocalize the function call and say something like:
    "num, calling num less than 5"
    "num, index, calling new Num equals num, InPlace as num equals index"

    -Mathew

    Saturday, January 14, 2006 1:04 AM