05 functional programming
TRANSCRIPT
![Page 1: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/1.jpg)
Functional Programming Ideas in C#
![Page 2: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/2.jpg)
What’s wrong with imperative code?
![Page 3: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/3.jpg)
What’s wrong with imperative code?
![Page 4: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/4.jpg)
What’s wrong with imperative code?
![Page 5: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/5.jpg)
What’s wrong with imperative code?
• It is repetitive• It is lengthy• (higher chances to introduce bugs)• (harder to read)• It describes what to do and how• (mixing low-level and high-level
concepts)
![Page 6: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/6.jpg)
Let’s try to solve these problems
![Page 7: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/7.jpg)
Delegate declaration
![Page 8: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/8.jpg)
Invocation
![Page 9: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/9.jpg)
Invocation (without noise)
![Page 10: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/10.jpg)
Delegates are immutable
You can only change the delegate variable
![Page 11: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/11.jpg)
Delegate knows its target
![Page 12: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/12.jpg)
Look, we manipulate code
as if it was data!
Nothing new though(we had interfaces for
ages)
![Page 13: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/13.jpg)
So we can • Store it in variable• Store it in a field or property• Pass it as a method parameter• Return it as a method result
![Page 14: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/14.jpg)
Functions (delegates) are first class citizens
Both pure and impure
![Page 15: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/15.jpg)
Pure vs side-effects
• Pure functions:–Maht.Sin(X)– String.Length– List<T>.IndexOf(T)
• Functions with side effects:– List<T>.Add(T)– DateTime.UtcNow– Console.WriteLine()
![Page 16: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/16.jpg)
Questions so far?
![Page 17: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/17.jpg)
Consider the problem
• Initiate processing• When some condition occurs, we
want to execute some code
![Page 18: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/18.jpg)
Pass delegates
![Page 19: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/19.jpg)
Call event
![Page 20: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/20.jpg)
Event syntax
![Page 21: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/21.jpg)
Nothing really complicated so far
Let’s go deeper
![Page 22: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/22.jpg)
![Page 23: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/23.jpg)
Separate method -- not needed
![Page 24: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/24.jpg)
Lambda expressions(lambda functions)
![Page 25: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/25.jpg)
Return value from delegate
![Page 26: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/26.jpg)
Let’s recall generics!
![Page 27: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/27.jpg)
Questions so far?
![Page 28: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/28.jpg)
How to make it more flexible?
![Page 29: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/29.jpg)
This is called closure (lexical closure)
The function captures variables from the scope
![Page 30: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/30.jpg)
Prefix
![Page 31: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/31.jpg)
Pitfall
![Page 32: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/32.jpg)
Function can even capture itself
![Page 33: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/33.jpg)
Function factory
![Page 34: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/34.jpg)
Generate IDs
![Page 35: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/35.jpg)
We’ve looked at examples of
higher-order functions
Theytake one or more functions as an input
or return a function
![Page 36: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/36.jpg)
Be comfortable with creating and passing lambdas
Understand closures
This is very powerful and widely used idiom
![Page 37: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/37.jpg)
Let’s improve imperative code
![Page 38: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/38.jpg)
Find element
![Page 39: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/39.jpg)
Find element
![Page 40: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/40.jpg)
Filter
![Page 41: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/41.jpg)
Filter
![Page 42: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/42.jpg)
Currying
Haskell Curry
Transform f: (X * Y) -> Z
into f: X -> (Y -> Z)
Input: Function with N parameters
Output: chain of N functions with 1 parameter each
![Page 43: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/43.jpg)
Currying
![Page 44: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/44.jpg)
How to compute Fibonacci(N)
What’s wrong with this?
It is exponential(slow)
link
![Page 45: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/45.jpg)
Memoization
![Page 46: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/46.jpg)
Memoization
![Page 47: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/47.jpg)
Main ideas
• Functions as first class citizens• Compose complex functions from
simple• Create specific functions out of
generic ones• More generic and reusable code with
higher order functions• For less code and less bugs
![Page 48: 05 functional programming](https://reader035.vdocument.in/reader035/viewer/2022062319/554ebf4bb4c905064d8b47fc/html5/thumbnails/48.jpg)
Questions?