Go Back   CodeCall Programming Forum > Software Development > Tutorials > CSharp Tutorials
Register Blogs Search Today's Posts Mark Forums Read

CSharp Tutorials Tutorials for C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-09-2009, 05:54 PM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Cool What are lambda expressions?

What are lambda expressions?
This is a question I got from Siten some time ago. I wont hide that I hate this one topic. Here are some code shortages you can get by using lambdas.

Lambdas are used instead of delegates, as shortened replacement for them. Many possible usages of lamdas lie in methods of collections, when you want to select some elements, or check is some element or all elements comply with your demands. For a start we need a list:

PHP Code:
    List<stringoriginalList = new List<string>()
    {
        
"123a""""aabb""zzxxa""ccccdd""so on a""etc""...a",
    }; 
Then we can use lambda expressions with methods that require delegates. For example Find, FindAll, Exists, TrueForAll as well as Any, All and some others. The examples below do the same, the only difference is that lambdas-code is shorter and more readable.

PHP Code:
    List<stringselectiveInclusion 
        
originalList.FindAll(new Predicate<string>(delegate(string everyobj)
            {
                return 
everyobj.Contains("a");
            }));

    List<
stringselectiveInclusion2 =
        
originalList.FindAll( (string everyobj) =>
            {
                return 
everyobj.Contains("a");
            }); 
Here is another example of lambdas. The difference is the same, in the line of method call.

PHP Code:
    bool isThere123 
        
originalList.Exists(new Predicate<string>(delegate(string s)
            {
                return 
== "123a";
            }));

    
bool isThere123lambda 
        
originalList.Exists( (string s) =>
            {
                return 
== "123a";
            }); 
The full source code is attached. Feel free to play with it.
Regards,
Arek Bulski.
Attached Files
File Type: zip LambdaExample.zip (37.7 KB, 20 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2009, 08:12 PM
John's Avatar
Co-Administrator
 
Join Date: Jul 2006
Age: 21
Posts: 5,835
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Re: What are lambda expressions?

I still haven't figured out what lambda expressions have to do with lambda calculus?
__________________

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2009, 08:47 PM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Re: What are lambda expressions?

Quote:
Originally Posted by John View Post
I still haven't figured out what lambda expressions have to do with lambda calculus?
There is some explanation on Wikipedia. I dont understand English well enough to understand it though. Lambda calculus - Wikipedia, the free encyclopedia
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-19-2009, 04:39 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: What are lambda expressions?

Just read this one. Never even saw it before now. Nice tutorial, +rep.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-14-2009, 08:46 PM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Re: What are lambda expressions?

Thank you, Jordan. Do you think many people just do not use Lambda expressions at all?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
lambda



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Back to Basics: Delegates, Anonymous Methods and Lambda Expressions Kernel News 0 11-27-2008 02:30 PM
Object Oriented F# - Encapsulation with Object Expressions Kernel News 0 11-27-2008 02:30 AM
Regular Expressions John PHP Tutorials 27 09-11-2008 03:25 PM
Async Computation Expressions - Resource and Exception Management Kernel News 0 08-16-2008 05:50 PM
Task Parallel Library and Async Computation Expressions Kernel News 0 08-04-2008 10:29 AM


All times are GMT -5. The time now is 09:39 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0