Click here to load reader
Upload
mike-melusky
View
377
Download
0
Embed Size (px)
Citation preview
Fun with Lambda ExpressionsMichael Melusky - @mrjavascriptPhilly.NET Code Camp 2015.2 – Saturday October 10 2015
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
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
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
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#)
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
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);
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); }
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#
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
Lambda Expressions in C# Lambda expressions can be used with any delegate type in C#
(input parameters) => expression
Lambda Expressions in C# Examples:
(x, y) => x == y
(int x, string s) => s.Length > x
() => SomeMethod()
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;
C# Expression Trees Example: expression trees
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
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()
Person Sort in Java Example: Sorting a list of objects using Comparator<T>
Person Sort in C# Example: Same example in C# using List.Sort(Comparison<T>)
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
Questions? Thank you for coming.
twitter/mrjavascript github/mrjavascript slideshare/mrjavascript