2. About the Speaker
Michael Melusky
Software Engineer for Audacious Inquiry in Baltimore, MD
Instructor at Penn State Harrisburg and ITT Technical Institute
Enjoys cooking, baseball and video games
3. Motivation and goals for the talk
I’m primarily a “Java Developer” by trade
Lambda Expressions were newly added in Java SE 8
Overview of what lambda expressions are
Overview of what delegates and anonymous methods are
Show how anonymous methods differ from lambda expressions
Show how the C# and Java implementations differ from each other
4. What is a Lambda
A lambda is a function
Originates from lambda calculus, expressing computation using variable binding
and substitution
A function is a computation which takes parameters and returns a value
A lambda enables functions to be passed around or stored like data
5. Lambdas in .NET
Popularized by LINQ (.NET language-integrated query)
Example: Test Scores with LINQ
Example: Even numbers in a list (C# with System.Linq.Enumerable)
Example: Even numbers in a list (F#)
6. Functional Programming Paradigm
Transition from imperative programming to functional programming
“Not what to do but how to do it”
Functions are first order data types
C# produced the ability to create anonymous functions, inline statements or
expressions whenever a delegate is expected
7. Evolution of C# Delegates
In C# 1.0, delegates can be instantiated by initialized with a method defined in the
code
delegate void MyDelegate(string s);
Static void Foo(string s) { Console.WriteLine(s); }
MyDelegate del = new MyDelegate(Foo);
8. Evolution of C# Delegates
C# 2.0 allowed for anonymous methods as a way to write inline blocks that could be
executed in delegate invocation
MyDelegate M
= delegate(string s) { Console.WriteLine(s); }
9. Evolution of C# Delegates
In C# 3.0 and above, delegates can now be initialized with a lambda expression:
MyDelegate M =
(x) => { Console.WriteLine(s) };
Example: Delegates in C#
10. C# Delegate Types
Action<T>
Delegate method that takes zero or more input parameters
Does not return a type
Func<TResult>
Delegate method that takes zero or more input parameters
Returns a value or reference
Predicate<T>
Delegate similar to Func except it returns a boolean
11. Lambda Expressions in C#
Lambda expressions can be used with any delegate type in C#
(input parameters) => expression
12. Lambda Expressions in C#
Examples:
(x, y)
=> x == y
(int x, string s)
=> s.Length > x
()
=> SomeMethod()
13. C# Expression Trees
(a,b) => a+b;
C# compiler translates lambda expressions into expression trees at MSIL time
(Microsoft Intermediate Language)
Expression<Func<int, int, int>> lambda = (a,b) => a + b;
15. C# Expression Trees
ParameterExpression num1 = Expression.Parameter(typeof(int), "num1");
ParameterExpression num2 = Expression.Parameter(typeof(int), "num2");
//Create the expression parameters
ParameterExpression[] parameters = new ParameterExpression[] { num1, num2 };
//Create the expression body
BinaryExpression body = Expression.Add(num1, num2);
//Create the expression
Expression<Func<int, int, int>> expression = Expression.Lambda<Func<int, int, int>>(body, parameters);
// Compile the expression
Func<int, int, int> compiledExpression = expression.Compile();
// Execute the expression.
int result = compiledExpression(3, 4); //return 7
16. Lambda Expressions in Java
Recently added as a feature in Java SE 8
The following interfaces now support lambda:
Iterable.forEach(lambda)
Collection.removeIf(lambda)
List.replaceAll(lambda)
List.sort(lambda)
Replaces Collections.sort()
17. Person Sort in Java
Example: Sorting a list of objects using Comparator<T>
18. Person Sort in C#
Example: Same example in C# using List.Sort(Comparison<T>)
19. In Summary
Lambdas are unnamed, inline functions
Lambda expressions can be used anywhere a delegate is required to keep your code
encapsulated
You can chain methods together to perform multiple operations
In VB, you cannot use lambda expressions as action delegates
20. Questions?
Thank you for coming.
twitter/mrjavascript
github/mrjavascript
slideshare/mrjavascript