SlideShare a Scribd company logo
Fun with Lambda
Expressions
Michael Melusky - @mrjavascript
Central Penn .NET – Tuesday May 19 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
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
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

More Related Content

What's hot (19)

PDF
C intro
SHIKHA GAUTAM
 
PPT
Create and analyse programs
Dr. C.V. Suresh Babu
 
PDF
Definitions of Functional Programming
Philip Schwarz
 
DOCX
First approach in linq
Vignesh Nethaji
 
PPTX
C# Delegates
Raghuveer Guthikonda
 
PDF
Implicit conversion and parameters
Knoldus Inc.
 
PPTX
JavaScript – ECMAScript Basics By Satyen
Satyen Pandya
 
PPT
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
PPT
Python Built-in Functions and Use cases
Srajan Mor
 
PPTX
Lexical analysis-using-lex
Dattatray Gandhmal
 
PPTX
Functions in python slide share
Devashish Kumar
 
PPTX
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
PPTX
Functions in Python
Shakti Singh Rathore
 
PPTX
Function overloading and overriding
Rajab Ali
 
PPTX
Introduction to Python Basics
Raghunath A
 
PDF
Under the hood of scala implicits (Scala eXchange 2014)
Alexander Podkhalyuzin
 
PDF
New lambdas
Wiem Zine Elabidine
 
PDF
Syntax directed translation
Akshaya Arunan
 
PDF
Control structures functions and modules in python programming
Srinivas Narasegouda
 
C intro
SHIKHA GAUTAM
 
Create and analyse programs
Dr. C.V. Suresh Babu
 
Definitions of Functional Programming
Philip Schwarz
 
First approach in linq
Vignesh Nethaji
 
C# Delegates
Raghuveer Guthikonda
 
Implicit conversion and parameters
Knoldus Inc.
 
JavaScript – ECMAScript Basics By Satyen
Satyen Pandya
 
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
Python Built-in Functions and Use cases
Srajan Mor
 
Lexical analysis-using-lex
Dattatray Gandhmal
 
Functions in python slide share
Devashish Kumar
 
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
Functions in Python
Shakti Singh Rathore
 
Function overloading and overriding
Rajab Ali
 
Introduction to Python Basics
Raghunath A
 
Under the hood of scala implicits (Scala eXchange 2014)
Alexander Podkhalyuzin
 
New lambdas
Wiem Zine Elabidine
 
Syntax directed translation
Akshaya Arunan
 
Control structures functions and modules in python programming
Srinivas Narasegouda
 

Viewers also liked (15)

PPTX
An evening with Angular 2
Mike Melusky
 
PPTX
C# in depth
Arnon Axelrod
 
PPTX
Java 8 Lambda Expressions
Hyderabad Scalability Meetup
 
PPS
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
PPTX
Fun with lambda expressions
Mike Melusky
 
PPTX
Building Native “apps” with Visual Studio 2015
Mike Melusky
 
PPTX
Ember.js and .NET Integration
Mike Melusky
 
PPTX
Emberjs and ASP.NET
Mike Melusky
 
PPTX
An evening with querydsl
Mike Melusky
 
PPTX
Fun with windows services
Mike Melusky
 
PPTX
Securing your azure web app with asp.net core data protection
Mike Melusky
 
PDF
Java 8 Lambda Expressions
Scott Leberknight
 
PPTX
The Dark Side Of Lambda Expressions in Java 8
Takipi
 
PPTX
An afternoon with angular 2
Mike Melusky
 
PPTX
AppDynamics VS New Relic – The Complete Guide
Takipi
 
An evening with Angular 2
Mike Melusky
 
C# in depth
Arnon Axelrod
 
Java 8 Lambda Expressions
Hyderabad Scalability Meetup
 
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
Fun with lambda expressions
Mike Melusky
 
Building Native “apps” with Visual Studio 2015
Mike Melusky
 
Ember.js and .NET Integration
Mike Melusky
 
Emberjs and ASP.NET
Mike Melusky
 
An evening with querydsl
Mike Melusky
 
Fun with windows services
Mike Melusky
 
Securing your azure web app with asp.net core data protection
Mike Melusky
 
Java 8 Lambda Expressions
Scott Leberknight
 
The Dark Side Of Lambda Expressions in Java 8
Takipi
 
An afternoon with angular 2
Mike Melusky
 
AppDynamics VS New Relic – The Complete Guide
Takipi
 
Ad

Similar to Fun with lambda expressions (20)

DOCX
Mastering C# Lambda Expressions: A Complete Guide
LetsUpdateSkills
 
PPTX
Evolution of C# delegates
mbaric
 
PDF
New c sharp3_features_(linq)_part_ii
Nico Ludwig
 
PPT
Csharp4 delegates lambda_and_events
Abed Bukhari
 
PPTX
Lamdha.pptx
ControlUrgentSecurit
 
PDF
Linq & lambda overview C#.net
Dhairya Joshi
 
PPTX
Functional Programming In Jdk8
Bansilal Haudakari
 
PDF
Lambda Expression For anyone that needs Java Lambda notes
shreyansh15388
 
PPTX
Lambdas and Extension using VS2010
vgrondin
 
PDF
Java 8 Lambda Expressions & Streams
NewCircle Training
 
PPTX
Java Lambda Expressions.pptx
SameerAhmed593310
 
PDF
From delegates, to lamdas and expression trees your guide to elegant code in ...
Credera
 
PDF
Project Lambda, JSR 335
Martin Skurla
 
PDF
LambdaExpressionInjavaforlearning fucntion
ShivamMishra465316
 
PPTX
Lambdas, Collections Framework, Stream API
Prabu U
 
PPTX
Java 8 features
Maýur Chourasiya
 
PDF
Jf12 lambdas injava8-1
langer4711
 
ODP
Can't Dance The Lambda
Togakangaroo
 
ODP
Method Handles in Java
hendersk
 
PDF
Hanoi JUG: Java 8 & lambdas
Benoît de CHATEAUVIEUX
 
Mastering C# Lambda Expressions: A Complete Guide
LetsUpdateSkills
 
Evolution of C# delegates
mbaric
 
New c sharp3_features_(linq)_part_ii
Nico Ludwig
 
Csharp4 delegates lambda_and_events
Abed Bukhari
 
Linq & lambda overview C#.net
Dhairya Joshi
 
Functional Programming In Jdk8
Bansilal Haudakari
 
Lambda Expression For anyone that needs Java Lambda notes
shreyansh15388
 
Lambdas and Extension using VS2010
vgrondin
 
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Java Lambda Expressions.pptx
SameerAhmed593310
 
From delegates, to lamdas and expression trees your guide to elegant code in ...
Credera
 
Project Lambda, JSR 335
Martin Skurla
 
LambdaExpressionInjavaforlearning fucntion
ShivamMishra465316
 
Lambdas, Collections Framework, Stream API
Prabu U
 
Java 8 features
Maýur Chourasiya
 
Jf12 lambdas injava8-1
langer4711
 
Can't Dance The Lambda
Togakangaroo
 
Method Handles in Java
hendersk
 
Hanoi JUG: Java 8 & lambdas
Benoît de CHATEAUVIEUX
 
Ad

More from Mike Melusky (13)

PPTX
Container Orchestration for .NET Developers
Mike Melusky
 
PPTX
Containerize all the things!
Mike Melusky
 
PPTX
Building a Google Cloud Firestore API with dotnet core
Mike Melusky
 
PPTX
Effective .NET Core Unit Testing with SQLite and Dapper
Mike Melusky
 
PPTX
Effective .NET Core Unit Testing with SQLite and Dapper
Mike Melusky
 
PPTX
Reactive Web Development with Spring Boot 2
Mike Melusky
 
PPTX
Building xamarin.forms apps with prism and mvvm
Mike Melusky
 
PPTX
Introduction to react native with redux
Mike Melusky
 
PPTX
Xamarin.Forms Bootcamp
Mike Melusky
 
PPTX
An evening with React Native
Mike Melusky
 
PPTX
Progressive Web Apps and React
Mike Melusky
 
PPTX
Into to Docker (Central PA Java User Group - 8/14/2017)
Mike Melusky
 
ODP
Philly.NET Code Camp 2014.1
Mike Melusky
 
Container Orchestration for .NET Developers
Mike Melusky
 
Containerize all the things!
Mike Melusky
 
Building a Google Cloud Firestore API with dotnet core
Mike Melusky
 
Effective .NET Core Unit Testing with SQLite and Dapper
Mike Melusky
 
Effective .NET Core Unit Testing with SQLite and Dapper
Mike Melusky
 
Reactive Web Development with Spring Boot 2
Mike Melusky
 
Building xamarin.forms apps with prism and mvvm
Mike Melusky
 
Introduction to react native with redux
Mike Melusky
 
Xamarin.Forms Bootcamp
Mike Melusky
 
An evening with React Native
Mike Melusky
 
Progressive Web Apps and React
Mike Melusky
 
Into to Docker (Central PA Java User Group - 8/14/2017)
Mike Melusky
 
Philly.NET Code Camp 2014.1
Mike Melusky
 

Recently uploaded (20)

PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 

Fun with lambda expressions

  • 1. Fun with Lambda Expressions Michael Melusky - @mrjavascript Central Penn .NET – Tuesday May 19 2015
  • 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 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
  • 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;
  • 14. C# Expression Trees Example: expression trees
  • 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