20
Fun with Lambda Expressions Michael Melusky - @mrjavascript Central Penn .NET – Tuesday May 19 2015

Fun with lambda expressions

Embed Size (px)

Citation preview

Page 1: Fun with lambda expressions

Fun with Lambda ExpressionsMichael Melusky - @mrjavascriptCentral Penn .NET – Tuesday May 19 2015

Page 2: Fun with lambda expressions

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

Page 3: Fun with lambda expressions

Motivation and goals for the talk I’m primarily a “Java Developer” by trade

Lamda 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

Page 4: Fun with lambda expressions

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

Page 5: Fun with lambda expressions

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#)

Page 6: Fun with lambda expressions

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

Page 7: Fun with lambda expressions

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);

Page 8: Fun with lambda expressions

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); }

Page 9: Fun with lambda expressions

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#

Page 10: Fun with lambda expressions

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

Page 11: Fun with lambda expressions

Lambda Expressions in C# Lambda expressions can be used with any delegate type in C#

(input parameters) => expression

Page 12: Fun with lambda expressions

Lambda Expressions in C# Examples:

(x, y) => x == y

(int x, string s) => s.Length > x

() => SomeMethod()

Page 13: Fun with lambda expressions

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;

Page 14: Fun with lambda expressions

C# Expression Trees Example: expression trees

Page 15: Fun with lambda expressions

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

Page 16: Fun with lambda expressions

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()

Page 17: Fun with lambda expressions

Person Sort in Java Example: Sorting a list of objects using Comparator<T>

Page 18: Fun with lambda expressions

Person Sort in C# Example: Same example in C# using List.Sort(Comparison<T>)

Page 19: Fun with lambda expressions

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

Page 20: Fun with lambda expressions

Questions? Thank you for coming.

twitter/mrjavascript

github/mrjavascript

slideshare/mrjavascript