Functional Alchemy

2,259 views

Published on

Slides from my presentation on Functional Alchemy at DDD SouthWest (and other places)

Published in: Technology
  • Be the first to comment

Functional Alchemy

  1. 1. Functional Alchemy 10 Some tricks to keep your C# DRY E: mark@dotnetsolutions.co.uk T: @markrendle B: blog.markrendle.net
  2. 2. Get all the code: bit.ly/functionalalchemy (goes to bitbucket.org)
  3. 3. Functional?
  4. 4. First-class functions
  5. 5. Action<T> Func<T, TResult>
  6. 6. Higher-order functions
  7. 7. Lazy evaluation
  8. 8. Immutability
  9. 9. Immutability Purity
  10. 10. Immutability Purity Tail-call optimisation
  11. 11. Immutability Purity Tail-call optimisation
  12. 12. Immutability Purity Tail-call optimisation (irrelevant bobbins)
  13. 13. Functions as objects
  14. 14. IComparer<T> Demo
  15. 15. Tuples
  16. 16. Tuple<T1,T2>
  17. 17. F# success, i = int.TryParse(“42”)
  18. 18. C# int i; bool success = int.TryParse(“42”, out i);
  19. 19. Tupleize Demo
  20. 20. C# var tuple = IntTryParse(“42”); if (tuple.Item1) { ... }
  21. 21. Tuple extensions Demo
  22. 22. Fixing C# and the .NET BCL
  23. 23. var handler = MyEvent; if (handler != null) { handler(this, EventArgs.Empty); }
  24. 24. EventHandler.Raise Demo
  25. 25. Higher-order functions
  26. 26. EventHandler.Raise (Again)
  27. 27. Better Exception handling
  28. 28. try { File.OpenRead(“foo.txt”); } catch (FileNotFoundException) { Error(“Cannot open file”); } catch (DirectoryNotFoundException) { Error(“Cannot open file”); } catch (UnauthorizedAccessException) { //etc, etc }
  29. 29. Multi-exception catch Demo
  30. 30. Augmenting functions
  31. 31. Cached (deterministic) methods Demo
  32. 32. Event-based Async Pattern Demo
  33. 33. Composition
  34. 34. Event-based Async Pattern + Cached methods Demo
  35. 35. Get all the code: bit.ly/functionalalchemy (goes to bitbucket.org) E: mark@dotnetsolutions.co.uk T: @markrendle B: blog.markrendle.net

×