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
 
PPT
C#3.0 & Vb 9.0 New Features
techfreak
 
PPTX
Introduction to F# 3.0
Talbott Crowell
 
DOCX
Project_Report (BARC-Jerin)_final
Jerin John
 
PPTX
C#-LINQ-and-Lambda-Expression
Simplilearn
 
PPTX
Lambdas, Collections Framework, Stream API
Prabu U
 
PPTX
6 assembly language computer organization
wewiv47743
 
PPTX
Programming in c++ ppt
sujathavvv
 
PPTX
Programming in c++ ppt
MalarMohana
 
PPTX
Machine Learning Pipelines - Joseph Bradley - Databricks
Spark Summit
 
PPTX
Chapter3: fundamental programming
Ngeam Soly
 
PDF
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
PDF
3-Python Python oho pytho hdiwefjhdsjhds
hardikbhagwaria83
 
PPTX
Introduction to c++ programming language
harinipradeep15
 
PPTX
Compiler Construction.pptx
BilalImran17
 
PPTX
Decided to go to the 65 and the value of the number
harshoberoi2050
 
PPTX
Templates1
zindadili
 
PPTX
Java 8 - An Overview
Indrajit Das
 
PPTX
C Programming - Basics of c -history of c
DHIVYAB17
 
PDF
The New Yorker cartoon premium membership of the
shubhamgupta7133
 
Mastering C# Lambda Expressions: A Complete Guide
LetsUpdateSkills
 
C#3.0 & Vb 9.0 New Features
techfreak
 
Introduction to F# 3.0
Talbott Crowell
 
Project_Report (BARC-Jerin)_final
Jerin John
 
C#-LINQ-and-Lambda-Expression
Simplilearn
 
Lambdas, Collections Framework, Stream API
Prabu U
 
6 assembly language computer organization
wewiv47743
 
Programming in c++ ppt
sujathavvv
 
Programming in c++ ppt
MalarMohana
 
Machine Learning Pipelines - Joseph Bradley - Databricks
Spark Summit
 
Chapter3: fundamental programming
Ngeam Soly
 
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
3-Python Python oho pytho hdiwefjhdsjhds
hardikbhagwaria83
 
Introduction to c++ programming language
harinipradeep15
 
Compiler Construction.pptx
BilalImran17
 
Decided to go to the 65 and the value of the number
harshoberoi2050
 
Templates1
zindadili
 
Java 8 - An Overview
Indrajit Das
 
C Programming - Basics of c -history of c
DHIVYAB17
 
The New Yorker cartoon premium membership of the
shubhamgupta7133
 
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
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
PDF
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PDF
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
PDF
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Practical Applications of AI in Local Government
OnBoard
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 

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