Your SlideShare is downloading. ×
05 functional programming
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

05 functional programming

1,385
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,385
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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