Coffeehouse Thread

17 posts

Lambda Expressions:Why?

Back to Forum: Coffeehouse
  • User profile image
    Secret​Software

    Hi guys,

    Lambda expressions are cool in the sense that you get to write few lines of code. What is the problem that existed before lambda expressions came along that made it nessessary to have lamda expression in a language like C#? What is the real use of it, and can we do without it? I mean why was it developed? I failed to see the real need for it except to help me write less lines of code.

    So why was it developed? Is it an evolution on Anonymous methods? why is it called "Lambda" expressions Lets talk about it and explore it.

  • User profile image
    littleguru

    Makes coding more fun and easier?

  • User profile image
    JohnAskew

    1) It will be nice to be able to use these expressions in dynamically compiled code; not having to go through iteration for a collection. With generic collections, this is even more powerful. It is very concise. The anonymous type would not be the cause for lambda expressions. Rhetorical questions: why do we need any other type of loop than a 'while'? Why was SQL created?



    http://encarta.msn.com

    lamb·da [ lámdə ] (plural lamb·das)
    noun 
    Definition:
     
    1. 11th letter of Greek alphabet: the 11th letter of the Greek alphabet, represented in the English alphabet as "l"
    2. junction in skull: the point of junction at the center of the back of the cranium between the rear plate of the cranium occipital bone and the two upper plates parietal bones. This junction is said to resemble the Greek capital letter lambda.
    [Early 17th century. < Greek]

    lamb·da cal·cu·lus
    noun 
    Definition:
     
    description of logical functions: a descriptive theory of mathematical functions and the way they combine, used as the basis for some high-level computer programming languages

  • User profile image
    Secret​Software

    JohnAskew wrote:
    
    1) It will be nice to be able to use these expressions in dynamically compiled code; not having to go through iteration for a collection. With generic collections, this is even more powerful. It is very concise. The anonymous type would not be the cause for lambda expressions. Rhetorical questions: why do we need any other type of loop than a 'while'? Why was SQL created?



    http://encarta.msn.com

    lamb·da [ lámdə ] (plural lamb·das)
    noun 
    Definition:
     
    1. 11th letter of Greek alphabet: the 11th letter of the Greek alphabet, represented in the English alphabet as "l"
    2. junction in skull: the point of junction at the center of the back of the cranium between the rear plate of the cranium occipital bone and the two upper plates parietal bones. This junction is said to resemble the Greek capital letter lambda.
    [Early 17th century. < Greek]

    lamb·da cal·cu·lus
    noun 
    Definition:
     
    description of logical functions: a descriptive theory of mathematical functions and the way they combine, used as the basis for some high-level computer programming languages


    So give me an example where using Lambda expression would make life easier for you as a developer , and compare that to life before lambda expressions. So far I have not encountered a situation where I imagine i would use it. But do show by example.

  • User profile image
    nwoolls

    I get the feeling that, more than anything, Lamda Expressions are an "enabling" technology, allowing for things like Linq.

  • User profile image
    Secret​Software

    nwoolls wrote:
    I get the feeling that, more than anything, Lamda Expressions are an "enabling" technology, allowing for things like Linq.


    But if that is the case, it would be used only internally at MS, and not be included with the .NET Framework 3.0.

    I am looking at old code , old programs I wrote, and trying to figure out where using lambda expressions would have been useful.

    So I still dont think that many developers, including myself, see real value in lambda expressions, but then again , being new technology, maybe its not well understood.

    I saw Andres Hijelsberg's webcast as he introduced LINQ, using Orcas CTP. He explained that they use Lambdas, to interact and query objects and let the compiler do the hard plumming work. But aside from that demo, I dont know where I am going to use it , or where its going to give me performance benefits as I think about future implementations for futrue projects I might work on.

  • User profile image
    JohnAskew

    Dynamically compiled code might begin its life as text in a database field.

    Perhaps a routine to select an item from a collection based on it's properties; those properties then may need to be parameterized to best and fully reuse the routine.

    This text will be quite concise via lambda expressions and anonymous type return values, and that text can be 'parameterized' after being pulled from a database field as a lambda expression.

    Once the desired lambda expression is formatted, it is compiled at runtime.

    So we have minimal effort to reuse code that is dynamically compiled.

    Without anonymous type return values, there is more to format. Without the lambda expression, there is more to format.
    Yes, it can all be done without either.

    One day, we will be able to speak to our computer and it will generate code, dynamically compile it and run it to give us our answers. Why not?

    So perhaps this technology is helping enable that scenario.


    All theory, no code...   examples...

  • User profile image
    Secret​Software

    JohnAskew wrote:
    

    Dynamically compiled code might begin its life as text in a database field.

    Perhaps a routine to select an item from a collection based on it's properties; those properties then may need to be parameterized to best and fully reuse the routine.

    This text will be quite concise via lambda expressions and anonymous type return values, and that text can be 'parameterized' after being pulled from a database field as a lambda expression.

    Once the desired lambda expression is formatted, it is compiled at runtime.

    So we have minimal effort to reuse code that is dynamically compiled.

    Without anonymous type return values, there is more to format. Without the lambda expression, there is more to format.
    Yes, it can all be done without either.

    One day, we will be able to speak to our computer and it will generate code, dynamically compile it and run it to give us our answers. Why not?

    So perhaps this technology is helping enable that scenario.


    All theory, no code...   examples...



    I see, I will wait and see until C9 gives us a video on Lambdas and their applications perhapse soon.

    As per your line of thinking, that way enterprises can "send their code" to their "minimal coded" client, and compile it at runtime, so that hackers and crackers would not be able to study it and produce cracked or corrupted versions of the software.

    Like banking software or things like that. If lambdas will enable this, and make it easier, then yeah I see a huge demand for this in the future.

    This will make downloaded programs smaller, as most of the code, will be downloaded at runtime and compiled, and then deleted as the program shuts down.

  • User profile image
    mawcc

    SecretSoftware wrote:
    
    nwoolls wrote: I get the feeling that, more than anything, Lamda Expressions are an "enabling" technology, allowing for things like Linq.


    But if that is the case, it would be used only internally at MS, and not be included with the .NET Framework 3.0.


    One example where you might need lambda expressions is to extend LINQ. Also remember that third party library developers often need some language features which the average application developer might not find useful in their daily work.

    BTW, this document describes the language features enabling LINQ in detail.

  • User profile image
    Rory

    SecretSoftware wrote:
    Hi guys,

    Lambda expressions are cool in the sense that you get to write few lines of code. What is the problem that existed before lambda expressions came along that made it nessessary to have lamda expression in a language like C#? What is the real use of it, and can we do without it? I mean why was it developed? I failed to see the real need for it except to help me write less lines of code.

    So why was it developed? Is it an evolution on Anonymous methods? why is it called "Lambda" expressions Lets talk about it and explore it.


    There will be many places that lambda expressions will make sense - we're already seeing a partial implementation of the idea in "anonymous methods."

    One place it might make your life easier is in the handling of events that aren't relevant to anything beyond themselves.

    That is, let's say you're writing a Windows app and you have a button, and that button is only for doing one thing, and that button is the only trigger for that one thing. You could either write an extra method to handle that logic, or you could just pass the logic along as either an anonymous method or a lambda expression to the wire-up code for the event handler.

    Whether you like that or not is a matter of style. It may not work for you, but others might love it.

    If you'd like to see real world examples of where lambda expressions might be useful, you ought to check out code from another language that has already had them for some time.

    For clarity, I'd suggest looking at some Python code. Just do a search for Python and lambda - you'll find something...

  • User profile image
    TommyCarlier

    I already use anonymous methods quite often. They come in very handy when you need to pass a delegate to Control.Invoke, or the Thread-constructor. Example:

    new Thread(new ThreadStart(delegate
    {
       while(x)
       {
          // perform some lengthy operation
          ...
          // update a progress bar on the UI thread

          lForm.BeginInvoke(new MethodInvoker(delegate
          {
             lProgressBar.Increment(1);
          }));
       }
    })).Start();

  • User profile image
    eddwo

    I used an anonymous method to keep a particular treeview node from being collapsed.

    Something like:

    TreeNode node = new TreeNode();
    node.OnBeforeCollapse += delegate(object sender,TreeViewCancelEventArgs e) { e.Cancel = true; }
    treeview.Nodes.Add(node);

    keeps the code all in one place.

  • User profile image
    staceyw

    I *love anonymous methods! They make async callbacks almost easy.  And local variable capture...need I say more?

  • User profile image
    Escamillo

    Anonymous methods are great in C# 2.0.  In fact, they seem as powerful as lambda expressions.  Just replace the word "delegate" with "lambda" and it looks like lisp. Wink

    But I'm not sure what lambda of C# 3.0 gives you over anonymous methods besides terser syntax and type inference regarding the parameters and return value.  Is type inference the main thing that lambda provides over anonymous methods, or is there something more (not that type inference is a trivial thing)?

  • User profile image
    Richard.Hein

    Escamillo wrote:
    Anonymous methods are great in C# 2.0.    Is type inference the main thing that lambda provides over anonymous methods, or is there something more (not that type inference is a trivial thing)?


    Yes, lamda expressions can be converted to and from expression trees.  This is the important thing, beyond anonymous delegates, etc....  It is the central feature because with lazy evaluation you can send off expression trees that can be optimized, changed, parallelized (Google PLINQ), etc.... 

  • User profile image
    Escamillo

    Richard.Hein wrote:
    
    Escamillo wrote: Anonymous methods are great in C# 2.0.    Is type inference the main thing that lambda provides over anonymous methods, or is there something more (not that type inference is a trivial thing)?


    Yes, lamda expressions can be converted to and from expression trees.  This is the important thing, beyond anonymous delegates, etc....  It is the central feature because with lazy evaluation you can send off expression trees that can be optimized, changed, parallelized (Google PLINQ), etc.... 



    Ah, I see.  Thanks. Smiley

  • User profile image
    TommyCarlier

    And it's these expression trees that are converted to SQL-code when using DLINQ. Without these, DLINQ would not be possible.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.