SlideShare a Scribd company logo
1 of 14
Lambda Expressions How to… YuriySeniuk http://yuriyseniuk.blogspot.com/
Why Lambda Expressions? Lambda expressions are simply methods; Give a shorter form of a methods; Speed up our work.
Using a Delegate public delegate int Arithmetic(int x); static public int Square(int x) {    return x * x; } Arithmetic myOperation= new Arithmetic (Square); Console.WriteLine("{0}", myOperation(5));
Using an Anonymous Methods public delegate int Arithmetic(int x); Arithmetic myOperation= new Arithmetic ( 		delegate 			{ return x * x; 			}); Console.WriteLine("{0}", myOperation(5));
public delegate int Arithmetic(int x); Arithmetic myOperation= x => x * x; Console.WriteLine("{0}", myOperation(5)); //Also with explicit input type Arithmetic myOperation = (int)x => x * x; Using a Lambda Expressions
Using λ with Two Arguments public delegate int Mul(int x, int y); MulmyOperation= (x, y) => x * y; Console.WriteLine("{0}", myOperation(5, 6)); //Also with explicit input type	 MulmyOperation = (int x, int y) => x * y; Console.WriteLine("{0}", myOperation(5, 6));
Using λ with Zero Arguments public delegate long Ticks(); Ticks myOperation= () => DateTime.Now.Ticks; Console.WriteLine("{0}", myOperation());
public delegate void DoNothing(); DoNothingmyOperation= () => {}; Using λ returns void
int[] source = new[] { 3, 8, 4, 6, 1, 7, 9, 2, 4, 8 }; foreach (int i in source.Where(        x =>        {            if (x % 2 == 0)                return true;            else if (x >= 7)                return true;            return false;        }    ))    Console.WriteLine(i); Complex body
How to pronounce If λ is a predicate x => x == “Predicate” => spoken as “such that”  Ex. x such that x state equals Predicate If λ is a projection returns a new type x => newSomeObject(x)  => Spoken as “becomes” Ex. x becomes new some object with name x.
The Func Delegate Types public delegate TR Func<TR>(); public delegate TR Func<T0, TR>(T0 a0); public delegate TR Func<T0, T1, TR>(T0 a0, T1 a1); public delegate TR Func<T0, T1, T2, TR>(T0 a0, T1 a1, T2 a2); public delegate TR Func<T0, T1, T2, T3, TR>(T0 a0, T1 a1, T2 a2, T3 a3); publicList<T> FilterList<T>( List<T> source, Func<T, bool> filter )     { List<T> list = new List<T>(); foreach( T l in source )       {         if ( filter( l ) ) list.Add( l );       }       return list;     }  List<int> list = new List<int>() { 3, 5, 7, 9, 5 }; List<int> filteredList = FilterList<int>( list, x => x > 5 );
The Action Delegate Types public delegate voidAction(); public delegate voidAction<T0>(T0 a0); public delegate voidAction<T0, T1>(T0 a0, T1 a1); public delegate voidAction<T0, T1, T2>(T0 a0, T1 a1, T2 a2); Public delegate voidAction<T0, T1, T2, T3>(T0 a0, T1 a1, T2 a2, T3 a3); void DoSomethingWithDelay(Action execute) { Thread.Sleep(1000); execute(); }  void F() {DoSomethingWithDelay( () => { Console.WriteLine("Done"); } ); }
Any questions.. ?
Source http://blogs.msdn.com/b/ericwhite/archive/2006/10/03/lambda-expressions.aspx http://msdn.microsoft.com/en-us/library/bb397687.aspx

More Related Content

What's hot (20)

Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Monad Transformers - Part 1
Monad Transformers - Part 1Monad Transformers - Part 1
Monad Transformers - Part 1
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
stack presentation
stack presentationstack presentation
stack presentation
 
C# quick ref (bruce 2016)
C# quick ref (bruce 2016)C# quick ref (bruce 2016)
C# quick ref (bruce 2016)
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Arrays
ArraysArrays
Arrays
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
 
Stacks
StacksStacks
Stacks
 
Templates
TemplatesTemplates
Templates
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Stacks
StacksStacks
Stacks
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Arrays
ArraysArrays
Arrays
 

Viewers also liked

Lambda expressions
Lambda expressionsLambda expressions
Lambda expressionsDoron Gold
 
My first experience with lambda expressions in java
My first experience with lambda expressions in javaMy first experience with lambda expressions in java
My first experience with lambda expressions in javaScheidt & Bachmann
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8bryanbibat
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jRajiv Gupta
 
Java 8 lambdas expressions
Java 8 lambdas expressionsJava 8 lambdas expressions
Java 8 lambdas expressionsLars Lemos
 
Simplifying java with lambdas (short)
Simplifying java with lambdas (short)Simplifying java with lambdas (short)
Simplifying java with lambdas (short)RichardWarburton
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8icarter09
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions Ganesh Samarthyam
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressionsMike Melusky
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzJAX London
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in JavaErhan Bagdemir
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java langer4711
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsEmiel Paasschens
 
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...Philip Schwarz
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Ganesh Samarthyam
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 

Viewers also liked (20)

Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
 
My first experience with lambda expressions in java
My first experience with lambda expressions in javaMy first experience with lambda expressions in java
My first experience with lambda expressions in java
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4j
 
Java 8 lambdas expressions
Java 8 lambdas expressionsJava 8 lambdas expressions
Java 8 lambdas expressions
 
Simplifying java with lambdas (short)
Simplifying java with lambdas (short)Simplifying java with lambdas (short)
Simplifying java with lambdas (short)
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressions
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian Goetz
 
SLF4J Explained........
SLF4J Explained........SLF4J Explained........
SLF4J Explained........
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
 
Reflection
ReflectionReflection
Reflection
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 

Similar to Lambda expressions

Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 
Linq Sanjay Vyas
Linq   Sanjay VyasLinq   Sanjay Vyas
Linq Sanjay Vyasrsnarayanan
 
Monadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsMonadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsChris Eargle
 
Linq And Its Impact On The.Net Framework
Linq And Its Impact On The.Net FrameworkLinq And Its Impact On The.Net Framework
Linq And Its Impact On The.Net Frameworkrushputin
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsAbed Bukhari
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mark Needham
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Skills Matter
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersTikal Knowledge
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.David Gómez García
 
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdfaristogifts99
 
Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMark Needham
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of LambdasEsther Lozano
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Codemotion
 
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...tdc-globalcode
 

Similar to Lambda expressions (20)

Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
Linq Sanjay Vyas
Linq   Sanjay VyasLinq   Sanjay Vyas
Linq Sanjay Vyas
 
Monadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsMonadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query Expressions
 
Linq And Its Impact On The.Net Framework
Linq And Its Impact On The.Net FrameworkLinq And Its Impact On The.Net Framework
Linq And Its Impact On The.Net Framework
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 
New C# features
New C# featuresNew C# features
New C# features
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
Linq introduction
Linq introductionLinq introduction
Linq introduction
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
 
Functional DDD
Functional DDDFunctional DDD
Functional DDD
 
Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented language
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
 
Smalltalk
SmalltalkSmalltalk
Smalltalk
 

Lambda expressions

  • 1. Lambda Expressions How to… YuriySeniuk http://yuriyseniuk.blogspot.com/
  • 2. Why Lambda Expressions? Lambda expressions are simply methods; Give a shorter form of a methods; Speed up our work.
  • 3. Using a Delegate public delegate int Arithmetic(int x); static public int Square(int x) {    return x * x; } Arithmetic myOperation= new Arithmetic (Square); Console.WriteLine("{0}", myOperation(5));
  • 4. Using an Anonymous Methods public delegate int Arithmetic(int x); Arithmetic myOperation= new Arithmetic ( delegate { return x * x; }); Console.WriteLine("{0}", myOperation(5));
  • 5. public delegate int Arithmetic(int x); Arithmetic myOperation= x => x * x; Console.WriteLine("{0}", myOperation(5)); //Also with explicit input type Arithmetic myOperation = (int)x => x * x; Using a Lambda Expressions
  • 6. Using λ with Two Arguments public delegate int Mul(int x, int y); MulmyOperation= (x, y) => x * y; Console.WriteLine("{0}", myOperation(5, 6)); //Also with explicit input type MulmyOperation = (int x, int y) => x * y; Console.WriteLine("{0}", myOperation(5, 6));
  • 7. Using λ with Zero Arguments public delegate long Ticks(); Ticks myOperation= () => DateTime.Now.Ticks; Console.WriteLine("{0}", myOperation());
  • 9. int[] source = new[] { 3, 8, 4, 6, 1, 7, 9, 2, 4, 8 }; foreach (int i in source.Where(        x =>        {            if (x % 2 == 0)                return true;            else if (x >= 7)                return true;            return false;        }    ))    Console.WriteLine(i); Complex body
  • 10. How to pronounce If λ is a predicate x => x == “Predicate” => spoken as “such that” Ex. x such that x state equals Predicate If λ is a projection returns a new type x => newSomeObject(x) => Spoken as “becomes” Ex. x becomes new some object with name x.
  • 11. The Func Delegate Types public delegate TR Func<TR>(); public delegate TR Func<T0, TR>(T0 a0); public delegate TR Func<T0, T1, TR>(T0 a0, T1 a1); public delegate TR Func<T0, T1, T2, TR>(T0 a0, T1 a1, T2 a2); public delegate TR Func<T0, T1, T2, T3, TR>(T0 a0, T1 a1, T2 a2, T3 a3); publicList<T> FilterList<T>( List<T> source, Func<T, bool> filter ) { List<T> list = new List<T>(); foreach( T l in source ) { if ( filter( l ) ) list.Add( l ); } return list; }  List<int> list = new List<int>() { 3, 5, 7, 9, 5 }; List<int> filteredList = FilterList<int>( list, x => x > 5 );
  • 12. The Action Delegate Types public delegate voidAction(); public delegate voidAction<T0>(T0 a0); public delegate voidAction<T0, T1>(T0 a0, T1 a1); public delegate voidAction<T0, T1, T2>(T0 a0, T1 a1, T2 a2); Public delegate voidAction<T0, T1, T2, T3>(T0 a0, T1 a1, T2 a2, T3 a3); void DoSomethingWithDelay(Action execute) { Thread.Sleep(1000); execute(); }  void F() {DoSomethingWithDelay( () => { Console.WriteLine("Done"); } ); }