05 functional programming

1,566 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,566
On SlideShare
0
From Embeds
0
Number of Embeds
1,190
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

05 functional programming

  1. 1. FunctionalProgramming Ideas in C#
  2. 2. What’s wrong with imperative code?
  3. 3. What’s wrong with imperative code?
  4. 4. What’s wrong with imperative code?
  5. 5. 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)
  6. 6. Let’s try to solve these problems
  7. 7. Delegate declaration
  8. 8. Invocation
  9. 9. Invocation (without noise)
  10. 10. Delegates are immutableYou can only change the delegate variable
  11. 11. Delegate knows its target
  12. 12. Look, we manipulate code as if it was data! Nothing new though(we had interfaces for ages)
  13. 13. 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
  14. 14. Functions (delegates)are first class citizensBoth pure and impure
  15. 15. 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()
  16. 16. Questions so far?
  17. 17. Consider the problem• Initiate processing• When some condition occurs, we want to execute some code
  18. 18. Pass delegates
  19. 19. Call event
  20. 20. Event syntax
  21. 21. Nothing really complicated so far Let’s go deeper
  22. 22. Separate method -- not needed
  23. 23. Lambda expressions (lambda functions)
  24. 24. Return value from delegate
  25. 25. Let’s recall generics!
  26. 26. Questions so far?
  27. 27. How to make it more flexible?
  28. 28. This is called closure (lexical closure)The function captures variables from the scope
  29. 29. Prefix
  30. 30. Pitfall
  31. 31. Function can even capture itself
  32. 32. Function factory
  33. 33. Generate IDs
  34. 34. We’ve looked at examples of higher-order functions Theytake one or more functions as an input or return a function
  35. 35. Be comfortable with creating and passing lambdas Understand closuresThis is very powerful and widely used idiom
  36. 36. Let’s improve imperative code
  37. 37. Find element
  38. 38. Find element
  39. 39. Filter
  40. 40. Filter
  41. 41. Currying Transform f: (X * Y) -> Z Haskell Curry into f: X -> (Y -> Z)Input: Function with N parametersOutput: chain of N functions with 1 parameter each
  42. 42. Currying
  43. 43. How to compute Fibonacci(N) What’s wrong with this? It is exponential (slow) link
  44. 44. Memoization
  45. 45. Memoization
  46. 46. 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
  47. 47. Questions?

×