SlideShare a Scribd company logo
M Sheik Uduman Ali Technical Lead iSOFT Plc [email_address] Let Us Learn
Procedural Programming int i = 0; i = i + 10; Output is deterministic  σ ΄ = f( σ ) Modifying the values of variables –  state σ  =  σ 0     σ 1     σ 2    …  σ n  =  σ  Sequential, conditional and repeated expression
Functional Programming Recursive functions – sequence is meaningless Variables are actually values // Procedural int Sum(int from, int to) { int result = 0; for(int i = from; i <= to; i++) result += i; return result; } // Recursive int Sum(int from, int to) { if(from > to) return 0; int result = Sum(from + 1, to); return from + result; }
Functional Programming Functions as values  Treated as simple objects like “Integer” can be passed as arguments, returned as results and  calculated with others High order functions Curried functions Immutable data structure MyList l0 = new MyList(); MyList l1 = l0.Add(100); MyList l2 = l1.Add(101); MyList result = l2.Add(102); MyList result = new MyList().Add(100).Add(101).Add(102); No assignments
Procedural: So What? No side-effect on expression evaluation More corresponds to mathematics Code can be more general problem solving by denotational semantics sub expression can be evaluated in any order parameterized
Code Comparison C int factorial(n) { int x = 1; while (n > 0) { x = x * n; n = n - 1; } return x; } F# let rec factorial num = match num with | n when n <= 1 -> 1 | n -> n * (factorial (n - 1)) fact(n) =    n! if n    0,      otherwise
A Debate Procedural:  I can use function pointers to treat “function as objects” Functional:  but you can’t create functions dynamically, and your function pointers are very limited. Procedural:  Me too support recursive..! Functional:  agreed. but everything is command driven as like as human approaches the computer.
A Debate Procedural:  What's in denotational semantics?  Functional:  for you, state based. It is changed from input to output. may fail to terminate, for example, while(true) {x=x;}.  alternative  predicate transformers are  goto, break and continue Procedural:  What's the benefit of “any order expression evaluation”? Functional:  I can do parallel programming simpler way
FP: Advantages  Shorter order of magnitude O(n) No assignments. Procedural programs consists 90% assignment statement Real modular programming Divide and conquer  No  goto  and  break .
Need for Lambda Calculus But in high order functions, insistence on naming is rather inconsistent, for example Define x and y by x = 2 and y = 4. Then xx = y Lambda notation allows to denote functions without naming  λx.t[x] for example,  λ x.x + 1  λ x.x 2 function argument fn. body
Lambda Calculus: Basic t is a lambda expression, if t = x where x    Var t =  λ x. M where x    Var and M is lambda expression t = MN where M and N are lambda expression  Three types of lambda expression: variables (referencing lambda expression) lambda abstractions (defining functions) applications (invoking functions)
Lambda Calculus: Basic In the expression  λ x.xy, x is bound variable (fall within the scope of  λ ) y is free variable (take the value from expression) Using lambda notation, traditional  f(x)  can be written as  f x . i.e. left association f x y  means  ( f ( x ) ) ( y )    λ x. λ y.t[x,y]  can be written as   λ x y.t[x,y] currying
Lambda Calculus Adoption Anonymous function name Type inference Parameterized types High order functions Immutability Recursion Currying Lazy evaluation
Lambda Calculus in .NET
Delegates Introduced in C# 1.0, much improved in C# 2.0  Dynamically wire up a method caller to its target method Two aspects: type and instance. delegate int Transformer(int x); static void Main(string[] args) { Transformer t = Square; int result = t(3); Console.WriteLine(result); Console.ReadLine(); } static int Square(int x) {return x * x;} type instance actually Transformer t = new Transformer(Square)
Anonymous Methods Introduced in C# 2.0 Define methods without name by delegate Compiler does closure-conversion  delegate int Transformer(int x); static void Main(string[] args) { Transformer t = delegate(int x) { return x * x; }; int result = t(3); Console.WriteLine(result); Console.ReadLine(); } LC1: Anonymous Function Name private static Transformer CS$<>9__CachedAnonymousMethodDelegate1; private static int <Main>b__0(int x) { return (x * x); } Compiler generated
Generics Introduced in C# 2.0, emphasized in C# 3.0 Way of reusability with a template Improved type safety  Reduce casting and boxing IEnumerable<int> myints =  new List<int> { 1, 1, 2, 3, 5, 8, 11 }; foreach (int i in myints) Console.Write(&quot;{0}\t&quot;, i);
Lambda Expression C# 3.0’s anonymous method Anonymous method written in place of a delegate instance. delegate int Transformer(int x); static void Main(string[] args) { Transformer t = x => return x * x; int result = t(3); Console.WriteLine(result); Console.ReadLine(); } Lighter syntax Implicit typed parameters LC2: Type inference
Generic Lambda Expression Very lighter syntax  For methods of any return type and any reasonable number of arguments. delegate TResult X <T>  (  ); delegate TResult X <T,TResult>  (T1 arg1); delegate TResult X <T1,T2,TResult>  (T1 arg1, T2 arg2); delegate TResult X <T1,T2,T3,..Tn,TResult> (T1 arg1, T2 arg2,  T3 arg3,...,Tn argn); Func<int, int> t = x => x * x; int result = t(4); Console.WriteLine(result); Console.ReadLine(); LC3: Parameterized type Func | Action
Generic Lambda Expression List<int> primes = new List<int>(); List<int> primeCubes = new List<int>(); primes.Add(2); primes.Add(3); primes.Add(5); primes.Add(7); primes.ForEach(x => primeCubes.Add(x * x * x)); foreach (int i in primeCubes) { Console.WriteLine(i); } LC4: High Order Function LC5: Immutability
Generic Lambda Expression Func<int, float, float> XPowerN = null; XPowerN = (n, x) => { if (n == 0) return 1.0f; else return x * XPowerN(n - 1, x); }; Func<float, float> Square = x => XPowerN(2, x); Func<float, float> Cube = x => XPowerN(3, x); Console.WriteLine(Square(5.0f).ToString()); Console.WriteLine(Cube(5.0f).ToString()); LC7: Currying LC6: Recursion
Expression Tree A data structure represents an expression Nodes as operands and operators 3 + (5 + 9) * 2 + 3 * + 5 9 2
Expression Tree Func<double, double, double> CylinderVolume =  (r, h) => 3.14 * r * r / 2; CylinderVolume(3, 9); Expression<Func<double, double, double>> XTCylinderVolume =  (r, h) => 3.14 * r * r / 2; XTCylinderVolume.Compile()(3, 9); Volume of a cylinder V =    radius 2  x height
IL Code for Delegate Lambda [CompilerGenerated] private static double <Main>b__0(double r, double h) { return (((3.14 * r) * r) / 2.0); } [CompilerGenerated] private static Func<double, double, double>  CS$<>9__CachedAnonymousMethodDelegate1; private static void Main(string[] args) { Func<double, double, double> CylinderVolume = delegate (double r, double h) { return ((3.14 * r) * r) / 2.0; }; }
IL Code for Expression Tree private static void Main(string[] args) { ParameterExpression CS$0$0000; ParameterExpression CS$0$0001; Expression<Func<double, double, double>>  XTCylinderVolume = Expression.Lambda<Func<double,  double, double>> (Expression.Divide(Expression.Multiply(Expression.Multiply(Expression.Constant(3.14, typeof(double)), CS$0$0000 = Expression.Parameter(typeof(double), &quot;radius&quot;)), CS$0$0001 = Expression.Parameter(typeof(double), &quot;height&quot;)), Expression.Constant(2.0, typeof(double))), new ParameterExpression[] { CS$0$0000, CS$0$0001 }); }
Expression Tree Func<double, double, double> CylinderVolume =  (r, h) => 3.14 * r * r / 2; CylinderVolume(3, 9); Expression<Func<double, double, double>> XTCylinderVolume =  (r, h) => 3.14 * r * r / 2; XTCylinderVolume.Compile()(3, 9); LC8: Lazy Evaluation Volume of a cylinder V =    radius 2  x height
Expression Tree Func<double, double, double> CylinderVolume =  (r, h) => 3.14 * r * r / 2; CylinderVolume(3, 9); Expression<Func<double, double, double>> XTCylinderVolume =  (r, h) => 3.14 * r * r / 2; XTCylinderVolume.Compile()(3, 9); LC8: Lazy Evaluation Volume of a cylinder V =    radius 2  x height
LINQ
LINQ Sample
Questions?

More Related Content

What's hot (20)

8 Pointers
8 Pointers8 Pointers
8 Pointers
Praveen M Jigajinni
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
Functions
FunctionsFunctions
Functions
Pragnavi Erva
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
Anton Kolotaev
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Philip Schwarz
 
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
Neeru Mittal
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
Praveen M Jigajinni
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Philip Schwarz
 
Recursion
RecursionRecursion
Recursion
James Wong
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
malaybpramanik
 
C function
C functionC function
C function
thirumalaikumar3
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 
Pointer
PointerPointer
Pointer
Learn By Watch
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
Anton Kolotaev
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Philip Schwarz
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
Neeru Mittal
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Philip Schwarz
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
malaybpramanik
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 

Viewers also liked (10)

C# in depth
C# in depthC# in depth
C# in depth
Arnon Axelrod
 
Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15
Murat Yener
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressions
Mike Melusky
 
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
Jaliya Udagedara
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
Anbarasan Gangadaran
 
Event+driven+programming key+features
Event+driven+programming key+featuresEvent+driven+programming key+features
Event+driven+programming key+features
Faisal Aziz
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
Ankit92Chitnavis
 
Introduction to Programming and QBasic Tutorial
Introduction to Programming and QBasic TutorialIntroduction to Programming and QBasic Tutorial
Introduction to Programming and QBasic Tutorial
nhomz
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
Salahaddin University-Erbil
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
Sudarsun Santhiappan
 
Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15
Murat Yener
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressions
Mike Melusky
 
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
Jaliya Udagedara
 
Event+driven+programming key+features
Event+driven+programming key+featuresEvent+driven+programming key+features
Event+driven+programming key+features
Faisal Aziz
 
Introduction to Programming and QBasic Tutorial
Introduction to Programming and QBasic TutorialIntroduction to Programming and QBasic Tutorial
Introduction to Programming and QBasic Tutorial
nhomz
 
Ad

Similar to Let Us Learn Lambda Using C# 3.0 (20)

Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
Yuriy Seniuk
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
Abed Bukhari
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06
HUST
 
C# programming
C# programming C# programming
C# programming
umesh patil
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
Skills Matter
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
Mark Needham
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
Abed Bukhari
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
Nico Ludwig
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
Togakangaroo
 
Testing for share
Testing for share Testing for share
Testing for share
Rajeev Mehta
 
It's Java Jim, But Not As We Know It!
It's Java Jim, But Not As We Know It!It's Java Jim, But Not As We Know It!
It's Java Jim, But Not As We Know It!
Simon Ritter
 
05 functional programming
05 functional programming05 functional programming
05 functional programming
Victor Matyushevskyy
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
Bansilal Haudakari
 
Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative Programmers
Chris
 
8. Functional Programming_updated(1).pptx
8. Functional Programming_updated(1).pptx8. Functional Programming_updated(1).pptx
8. Functional Programming_updated(1).pptx
jaymalachavan
 
Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambda
John Walsh
 
It's Java, Jim, but not as we know it
It's Java, Jim, but not as we know itIt's Java, Jim, but not as we know it
It's Java, Jim, but not as we know it
Simon Ritter
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
Kevlin Henney
 
C# labprograms
C# labprogramsC# labprograms
C# labprograms
Jafar Nesargi
 
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
techfreak
 
Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
Yuriy Seniuk
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
Abed Bukhari
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06
HUST
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
Skills Matter
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
Mark Needham
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
Abed Bukhari
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
Nico Ludwig
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
Togakangaroo
 
Testing for share
Testing for share Testing for share
Testing for share
Rajeev Mehta
 
It's Java Jim, But Not As We Know It!
It's Java Jim, But Not As We Know It!It's Java Jim, But Not As We Know It!
It's Java Jim, But Not As We Know It!
Simon Ritter
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
Bansilal Haudakari
 
Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative Programmers
Chris
 
8. Functional Programming_updated(1).pptx
8. Functional Programming_updated(1).pptx8. Functional Programming_updated(1).pptx
8. Functional Programming_updated(1).pptx
jaymalachavan
 
Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambda
John Walsh
 
It's Java, Jim, but not as we know it
It's Java, Jim, but not as we know itIt's Java, Jim, but not as we know it
It's Java, Jim, but not as we know it
Simon Ritter
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
Kevlin Henney
 
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
techfreak
 
Ad

Recently uploaded (20)

Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
GIS and FME: The Foundation to Improve the Locate Process of Utilities
GIS and FME: The Foundation to Improve the Locate Process of UtilitiesGIS and FME: The Foundation to Improve the Locate Process of Utilities
GIS and FME: The Foundation to Improve the Locate Process of Utilities
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0
RodrigoMori7
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
GIS and FME: The Foundation to Improve the Locate Process of Utilities
GIS and FME: The Foundation to Improve the Locate Process of UtilitiesGIS and FME: The Foundation to Improve the Locate Process of Utilities
GIS and FME: The Foundation to Improve the Locate Process of Utilities
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0FCF- Getting Started in Cybersecurity 3.0
FCF- Getting Started in Cybersecurity 3.0
RodrigoMori7
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 

Let Us Learn Lambda Using C# 3.0

  • 1. M Sheik Uduman Ali Technical Lead iSOFT Plc [email_address] Let Us Learn
  • 2. Procedural Programming int i = 0; i = i + 10; Output is deterministic σ ΄ = f( σ ) Modifying the values of variables – state σ = σ 0  σ 1  σ 2  … σ n = σ  Sequential, conditional and repeated expression
  • 3. Functional Programming Recursive functions – sequence is meaningless Variables are actually values // Procedural int Sum(int from, int to) { int result = 0; for(int i = from; i <= to; i++) result += i; return result; } // Recursive int Sum(int from, int to) { if(from > to) return 0; int result = Sum(from + 1, to); return from + result; }
  • 4. Functional Programming Functions as values Treated as simple objects like “Integer” can be passed as arguments, returned as results and calculated with others High order functions Curried functions Immutable data structure MyList l0 = new MyList(); MyList l1 = l0.Add(100); MyList l2 = l1.Add(101); MyList result = l2.Add(102); MyList result = new MyList().Add(100).Add(101).Add(102); No assignments
  • 5. Procedural: So What? No side-effect on expression evaluation More corresponds to mathematics Code can be more general problem solving by denotational semantics sub expression can be evaluated in any order parameterized
  • 6. Code Comparison C int factorial(n) { int x = 1; while (n > 0) { x = x * n; n = n - 1; } return x; } F# let rec factorial num = match num with | n when n <= 1 -> 1 | n -> n * (factorial (n - 1)) fact(n) = n! if n  0,  otherwise
  • 7. A Debate Procedural: I can use function pointers to treat “function as objects” Functional: but you can’t create functions dynamically, and your function pointers are very limited. Procedural: Me too support recursive..! Functional: agreed. but everything is command driven as like as human approaches the computer.
  • 8. A Debate Procedural: What's in denotational semantics? Functional: for you, state based. It is changed from input to output. may fail to terminate, for example, while(true) {x=x;}. alternative predicate transformers are goto, break and continue Procedural: What's the benefit of “any order expression evaluation”? Functional: I can do parallel programming simpler way
  • 9. FP: Advantages Shorter order of magnitude O(n) No assignments. Procedural programs consists 90% assignment statement Real modular programming Divide and conquer No goto and break .
  • 10. Need for Lambda Calculus But in high order functions, insistence on naming is rather inconsistent, for example Define x and y by x = 2 and y = 4. Then xx = y Lambda notation allows to denote functions without naming λx.t[x] for example, λ x.x + 1 λ x.x 2 function argument fn. body
  • 11. Lambda Calculus: Basic t is a lambda expression, if t = x where x  Var t = λ x. M where x  Var and M is lambda expression t = MN where M and N are lambda expression Three types of lambda expression: variables (referencing lambda expression) lambda abstractions (defining functions) applications (invoking functions)
  • 12. Lambda Calculus: Basic In the expression λ x.xy, x is bound variable (fall within the scope of λ ) y is free variable (take the value from expression) Using lambda notation, traditional f(x) can be written as f x . i.e. left association f x y means ( f ( x ) ) ( y )  λ x. λ y.t[x,y] can be written as λ x y.t[x,y] currying
  • 13. Lambda Calculus Adoption Anonymous function name Type inference Parameterized types High order functions Immutability Recursion Currying Lazy evaluation
  • 15. Delegates Introduced in C# 1.0, much improved in C# 2.0 Dynamically wire up a method caller to its target method Two aspects: type and instance. delegate int Transformer(int x); static void Main(string[] args) { Transformer t = Square; int result = t(3); Console.WriteLine(result); Console.ReadLine(); } static int Square(int x) {return x * x;} type instance actually Transformer t = new Transformer(Square)
  • 16. Anonymous Methods Introduced in C# 2.0 Define methods without name by delegate Compiler does closure-conversion delegate int Transformer(int x); static void Main(string[] args) { Transformer t = delegate(int x) { return x * x; }; int result = t(3); Console.WriteLine(result); Console.ReadLine(); } LC1: Anonymous Function Name private static Transformer CS$<>9__CachedAnonymousMethodDelegate1; private static int <Main>b__0(int x) { return (x * x); } Compiler generated
  • 17. Generics Introduced in C# 2.0, emphasized in C# 3.0 Way of reusability with a template Improved type safety Reduce casting and boxing IEnumerable<int> myints = new List<int> { 1, 1, 2, 3, 5, 8, 11 }; foreach (int i in myints) Console.Write(&quot;{0}\t&quot;, i);
  • 18. Lambda Expression C# 3.0’s anonymous method Anonymous method written in place of a delegate instance. delegate int Transformer(int x); static void Main(string[] args) { Transformer t = x => return x * x; int result = t(3); Console.WriteLine(result); Console.ReadLine(); } Lighter syntax Implicit typed parameters LC2: Type inference
  • 19. Generic Lambda Expression Very lighter syntax For methods of any return type and any reasonable number of arguments. delegate TResult X <T> ( ); delegate TResult X <T,TResult> (T1 arg1); delegate TResult X <T1,T2,TResult> (T1 arg1, T2 arg2); delegate TResult X <T1,T2,T3,..Tn,TResult> (T1 arg1, T2 arg2, T3 arg3,...,Tn argn); Func<int, int> t = x => x * x; int result = t(4); Console.WriteLine(result); Console.ReadLine(); LC3: Parameterized type Func | Action
  • 20. Generic Lambda Expression List<int> primes = new List<int>(); List<int> primeCubes = new List<int>(); primes.Add(2); primes.Add(3); primes.Add(5); primes.Add(7); primes.ForEach(x => primeCubes.Add(x * x * x)); foreach (int i in primeCubes) { Console.WriteLine(i); } LC4: High Order Function LC5: Immutability
  • 21. Generic Lambda Expression Func<int, float, float> XPowerN = null; XPowerN = (n, x) => { if (n == 0) return 1.0f; else return x * XPowerN(n - 1, x); }; Func<float, float> Square = x => XPowerN(2, x); Func<float, float> Cube = x => XPowerN(3, x); Console.WriteLine(Square(5.0f).ToString()); Console.WriteLine(Cube(5.0f).ToString()); LC7: Currying LC6: Recursion
  • 22. Expression Tree A data structure represents an expression Nodes as operands and operators 3 + (5 + 9) * 2 + 3 * + 5 9 2
  • 23. Expression Tree Func<double, double, double> CylinderVolume = (r, h) => 3.14 * r * r / 2; CylinderVolume(3, 9); Expression<Func<double, double, double>> XTCylinderVolume = (r, h) => 3.14 * r * r / 2; XTCylinderVolume.Compile()(3, 9); Volume of a cylinder V =  radius 2 x height
  • 24. IL Code for Delegate Lambda [CompilerGenerated] private static double <Main>b__0(double r, double h) { return (((3.14 * r) * r) / 2.0); } [CompilerGenerated] private static Func<double, double, double> CS$<>9__CachedAnonymousMethodDelegate1; private static void Main(string[] args) { Func<double, double, double> CylinderVolume = delegate (double r, double h) { return ((3.14 * r) * r) / 2.0; }; }
  • 25. IL Code for Expression Tree private static void Main(string[] args) { ParameterExpression CS$0$0000; ParameterExpression CS$0$0001; Expression<Func<double, double, double>> XTCylinderVolume = Expression.Lambda<Func<double, double, double>> (Expression.Divide(Expression.Multiply(Expression.Multiply(Expression.Constant(3.14, typeof(double)), CS$0$0000 = Expression.Parameter(typeof(double), &quot;radius&quot;)), CS$0$0001 = Expression.Parameter(typeof(double), &quot;height&quot;)), Expression.Constant(2.0, typeof(double))), new ParameterExpression[] { CS$0$0000, CS$0$0001 }); }
  • 26. Expression Tree Func<double, double, double> CylinderVolume = (r, h) => 3.14 * r * r / 2; CylinderVolume(3, 9); Expression<Func<double, double, double>> XTCylinderVolume = (r, h) => 3.14 * r * r / 2; XTCylinderVolume.Compile()(3, 9); LC8: Lazy Evaluation Volume of a cylinder V =  radius 2 x height
  • 27. Expression Tree Func<double, double, double> CylinderVolume = (r, h) => 3.14 * r * r / 2; CylinderVolume(3, 9); Expression<Func<double, double, double>> XTCylinderVolume = (r, h) => 3.14 * r * r / 2; XTCylinderVolume.Compile()(3, 9); LC8: Lazy Evaluation Volume of a cylinder V =  radius 2 x height
  • 28. LINQ