SlideShare a Scribd company logo
1 of 20
Fun with Lambda
Expressions
Michael Melusky - @mrjavascript
Philly.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

More Related Content

What's hot

Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxEng Teong Cheah
 
OOP in C++
OOP in C++OOP in C++
OOP in C++ppd1961
 
structured programming Introduction to c fundamentals
structured programming Introduction to c fundamentalsstructured programming Introduction to c fundamentals
structured programming Introduction to c fundamentalsOMWOMA JACKSON
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#Riccardo Terrell
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP ImplementationFridz Felisco
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Featurestechfreak
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 
Compiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statementsCompiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statementsGeo Marian
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationAkhil Kaushik
 
Procedural vs. object oriented programming
Procedural vs. object oriented programmingProcedural vs. object oriented programming
Procedural vs. object oriented programmingHaris Bin Zahid
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming hccit
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerVineet Kumar Saini
 
Programming in c++
Programming in c++Programming in c++
Programming in c++sujathavvv
 
Complete Tokens in c/c++
Complete Tokens in c/c++Complete Tokens in c/c++
Complete Tokens in c/c++Shobi P P
 
Programming of c++
Programming of c++Programming of c++
Programming of c++Ateeq Sindhu
 

What's hot (20)

Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic Syntax
 
Introduction to F# 3.0
Introduction to F# 3.0Introduction to F# 3.0
Introduction to F# 3.0
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
structured programming Introduction to c fundamentals
structured programming Introduction to c fundamentalsstructured programming Introduction to c fundamentals
structured programming Introduction to c fundamentals
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Compiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statementsCompiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statements
 
Pascal for beginers tute
Pascal for beginers   tutePascal for beginers   tute
Pascal for beginers tute
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
Procedural vs. object oriented programming
Procedural vs. object oriented programmingProcedural vs. object oriented programming
Procedural vs. object oriented programming
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Complete Tokens in c/c++
Complete Tokens in c/c++Complete Tokens in c/c++
Complete Tokens in c/c++
 
Advanced Java Topics
Advanced Java TopicsAdvanced Java Topics
Advanced Java Topics
 
Programming of c++
Programming of c++Programming of c++
Programming of c++
 

Viewers also liked

Ember.js and .NET Integration
Ember.js and .NET IntegrationEmber.js and .NET Integration
Ember.js and .NET IntegrationMike Melusky
 
Building Native “apps” with Visual Studio 2015
Building Native “apps” with Visual Studio 2015Building Native “apps” with Visual Studio 2015
Building Native “apps” with Visual Studio 2015Mike Melusky
 
Emberjs and ASP.NET
Emberjs and ASP.NETEmberjs and ASP.NET
Emberjs and ASP.NETMike Melusky
 
An evening with querydsl
An evening with querydslAn evening with querydsl
An evening with querydslMike Melusky
 
Fun with windows services
Fun with windows servicesFun with windows services
Fun with windows servicesMike Melusky
 
Securing your azure web app with asp.net core data protection
Securing your azure web app with asp.net core data protectionSecuring your azure web app with asp.net core data protection
Securing your azure web app with asp.net core data protectionMike Melusky
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressionsMike Melusky
 
An evening with Angular 2
An evening with Angular 2An evening with Angular 2
An evening with Angular 2Mike Melusky
 
An afternoon with angular 2
An afternoon with angular 2An afternoon with angular 2
An afternoon with angular 2Mike Melusky
 

Viewers also liked (9)

Ember.js and .NET Integration
Ember.js and .NET IntegrationEmber.js and .NET Integration
Ember.js and .NET Integration
 
Building Native “apps” with Visual Studio 2015
Building Native “apps” with Visual Studio 2015Building Native “apps” with Visual Studio 2015
Building Native “apps” with Visual Studio 2015
 
Emberjs and ASP.NET
Emberjs and ASP.NETEmberjs and ASP.NET
Emberjs and ASP.NET
 
An evening with querydsl
An evening with querydslAn evening with querydsl
An evening with querydsl
 
Fun with windows services
Fun with windows servicesFun with windows services
Fun with windows services
 
Securing your azure web app with asp.net core data protection
Securing your azure web app with asp.net core data protectionSecuring your azure web app with asp.net core data protection
Securing your azure web app with asp.net core data protection
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressions
 
An evening with Angular 2
An evening with Angular 2An evening with Angular 2
An evening with Angular 2
 
An afternoon with angular 2
An afternoon with angular 2An afternoon with angular 2
An afternoon with angular 2
 

Similar to Fun with Lambda Expressions in C# and Java

Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraJaliya Udagedara
 
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...Anil Sharma
 
Project_Report (BARC-Jerin)_final
Project_Report (BARC-Jerin)_finalProject_Report (BARC-Jerin)_final
Project_Report (BARC-Jerin)_finalJerin John
 
C#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionC#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionSimplilearn
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programmingNgeam Soly
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhHarmeet Singh(Taara)
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APIPrabu U
 
Machine Learning Pipelines - Joseph Bradley - Databricks
Machine Learning Pipelines - Joseph Bradley - DatabricksMachine Learning Pipelines - Joseph Bradley - Databricks
Machine Learning Pipelines - Joseph Bradley - DatabricksSpark Summit
 
Programming in c++ ppt
Programming in c++ pptProgramming in c++ ppt
Programming in c++ pptsujathavvv
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The LambdaTogakangaroo
 
Road to Dynamic LINQ Part 1
Road to Dynamic LINQ Part 1Road to Dynamic LINQ Part 1
Road to Dynamic LINQ Part 1Axilis
 
Compiler Construction.pptx
Compiler Construction.pptxCompiler Construction.pptx
Compiler Construction.pptxBilalImran17
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003Prashant odhavani
 

Similar to Fun with Lambda Expressions in C# and Java (20)

Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
 
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
 
Project_Report (BARC-Jerin)_final
Project_Report (BARC-Jerin)_finalProject_Report (BARC-Jerin)_final
Project_Report (BARC-Jerin)_final
 
C#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionC#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-Expression
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programming
 
Generics
GenericsGenerics
Generics
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
 
JDK8 Lambda expressions demo
JDK8 Lambda expressions demoJDK8 Lambda expressions demo
JDK8 Lambda expressions demo
 
Machine Learning Pipelines - Joseph Bradley - Databricks
Machine Learning Pipelines - Joseph Bradley - DatabricksMachine Learning Pipelines - Joseph Bradley - Databricks
Machine Learning Pipelines - Joseph Bradley - Databricks
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
Programming in c++ ppt
Programming in c++ pptProgramming in c++ ppt
Programming in c++ ppt
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
 
Road to Dynamic LINQ Part 1
Road to Dynamic LINQ Part 1Road to Dynamic LINQ Part 1
Road to Dynamic LINQ Part 1
 
Compiler Construction.pptx
Compiler Construction.pptxCompiler Construction.pptx
Compiler Construction.pptx
 
Linq
LinqLinq
Linq
 
Templates1
Templates1Templates1
Templates1
 
Of Lambdas and LINQ
Of Lambdas and LINQOf Lambdas and LINQ
Of Lambdas and LINQ
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
 

More from Mike Melusky

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

More from Mike Melusky (13)

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

Recently uploaded

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 

Recently uploaded (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 

Fun with Lambda Expressions in C# and Java

  • 1. Fun with Lambda Expressions Michael Melusky - @mrjavascript Philly.NET Code Camp 2015.2 – Saturday October 10 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 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;
  • 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