Successfully reported this slideshow.

Level Up Your Functional Programming Skills with LINQ

3

Share

Upcoming SlideShare
06 LINQ
06 LINQ
Loading in …3
×
1 of 84
1 of 84

Level Up Your Functional Programming Skills with LINQ

3

Share

Download to read offline

In this presentation, Cameron Presley shows you how to implement basic LINQ Operations leveraging Funcs, Generics, and Extension Methods. Once fundamentals are in place, Cameron starts building the various LINQ methods

In this presentation, Cameron Presley shows you how to implement basic LINQ Operations leveraging Funcs, Generics, and Extension Methods. Once fundamentals are in place, Cameron starts building the various LINQ methods

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Level Up Your Functional Programming Skills with LINQ

  1. 1. @pcameronpresley Level Up on FP Rebuilding LINQ Cameron Presley Cameron@TheSoftwareMentor.com
  2. 2. @pcameronpresley 2 Hello!
  3. 3. @pcameronpresley What is LINQ? Fundamentals Implementing the Trinity 3
  4. 4. 4
  5. 5. @pcameronpresley 5
  6. 6. @pcameronpresley 6 Which Delivery Teams Have More Than 7 Members?
  7. 7. @pcameronpresley 7
  8. 8. @pcameronpresley 8
  9. 9. @pcameronpresley “Set of operations that works on data independent of the source In addition, these operations are type safe, allowing the compiler to catch your mistakes sooner 9
  10. 10. @pcameronpresley Why LINQ? 10
  11. 11. @pcameronpresley What Makes LINQ Work? 1. Func 2. Generics 3. Extension Methods 11
  12. 12. @pcameronpresley Getting Functional with Funcs! 12
  13. 13. @pcameronpresley Background ◦ Introduced in C# 3.0 ◦ Type that when invoked returns a value ◦ Requires 0 or more parameters 13
  14. 14. @pcameronpresley
  15. 15. @pcameronpresley Func<DeliveryTeam, bool> Func<DeliveryTeam, string>
  16. 16. @pcameronpresley 16
  17. 17. @pcameronpresley 17
  18. 18. @pcameronpresley Writing General Code Generics 18
  19. 19. @pcameronpresley Background ◦ Introduced in C# 2.0 ◦ Allows for type parameters for various constructs ◦ Great for when the type doesn’t matter for implementation 19
  20. 20. @pcameronpresley 20
  21. 21. @pcameronpresley
  22. 22. @pcameronpresley 22
  23. 23. @pcameronpresley When Generics Make Sense When you’re writing the third structure in a row… Logic is the same, only the types are different 23
  24. 24. @pcameronpresley 24 Extending Type Functionality Extension Methods
  25. 25. @pcameronpresley Background ◦ Introduced in C# 3.0 ◦ Extends functionality to types without having to recompile or creating a new type ◦ Great for when working with types you don’t have access to 25
  26. 26. @pcameronpresley 26
  27. 27. @pcameronpresley 27 You Could Have Invented LINQ!
  28. 28. @pcameronpresley “The most common extension methods are the LINQ standard query operators that add query functionality to the existing System.Collections.IEnumerable and System.Collections.Generic.IEnumerable <T> types. 28 MS Docs on Extension Methods
  29. 29. 29
  30. 30. @pcameronpresley Design Guidelines ◦ Extend the IEnumerable type ◦ Same signatures of LINQ ◦ Same properties as LINQ 30
  31. 31. @pcameronpresley Changing Types with Map 31
  32. 32. @pcameronpresley Background Changes a collection of one type to another by using a mapper function Maintains the length of the collection 32
  33. 33. @pcameronpresley 33 Given a collection of DeliveryTeams, we want to see the names, so we write the following
  34. 34. @pcameronpresley 34
  35. 35. @pcameronpresley 35 Given a collection of DeliveryTeams, we want to see the TeamLeads, so we write the following
  36. 36. @pcameronpresley 36
  37. 37. @pcameronpresley 37
  38. 38. @pcameronpresley
  39. 39. @pcameronpresley 39 Given a collection of DeliveryTeams, we want to know the number of team members on each team
  40. 40. @pcameronpresley 40
  41. 41. @pcameronpresley 41
  42. 42. @pcameronpresley 42
  43. 43. @pcameronpresley 43 Any reason we need to constraint this to the DeliveryTeam type?
  44. 44. @pcameronpresley 44 Select Imp from GitHub
  45. 45. @pcameronpresley 45 Do we need to limit to List<TSource> and List<TResult>?
  46. 46. @pcameronpresley “Use Map to Transform the Collection Into The Data You Need 46
  47. 47. @pcameronpresley Keeping What’s Needed with Keep 47
  48. 48. @pcameronpresley Background Returns a subset of a collection based on a predicate Length is between 0 and original length Doesn’t return items that weren’t in the list Maintains order 48
  49. 49. 49 Which DeliveryTeams have more than 7 team members?
  50. 50. 50 Which DeliveryTeams have Dana as a TeamLead?
  51. 51. 51
  52. 52. 52
  53. 53. @pcameronpresley 53
  54. 54. @pcameronpresley 54 Any reason why this would be limited to DeliveryTeam?
  55. 55. 55
  56. 56. @pcameronpresley 56 Where Implementation from GitHub
  57. 57. @pcameronpresley “Use Keep to Keep Data That You Want 57
  58. 58. @pcameronpresley Going from many to one with Reduce 58
  59. 59. @pcameronpresley Background Takes a collection, an initial value, and a combiner method, returns a single value 59
  60. 60. @pcameronpresley 60 How many team members do we have in total?
  61. 61. @pcameronpresley 61
  62. 62. @pcameronpresley 62
  63. 63. @pcameronpresley 63
  64. 64. @pcameronpresley 64
  65. 65. @pcameronpresley 65 Any reason why this should only work on DeliveryTeam?
  66. 66. @pcameronpresley 66
  67. 67. @pcameronpresley 67 Aggregate Imp from GitHub
  68. 68. @pcameronpresley “Use Reduce to Transform a Collection to a Single Value by First Identifying the Initial Value, Followed by Writing the Combining Logic 68
  69. 69. @pcameronpresley Reducing LINQ to Reduce 69 No, Seriously…
  70. 70. @pcameronpresley Breaking Down The Problem ◦ Determine the Initial Value ◦ Determine how to combine the initial value with the next element 70
  71. 71. @pcameronpresley 71 What’s the initial value? What should we return if the source is empty? Enumerable<TResult>.Empty()
  72. 72. @pcameronpresley Setting Up Map via Reduce 72 1. Covert item from T to TResult 2. Append the converted item to values
  73. 73. @pcameronpresley Converting from T to TResult 73
  74. 74. @pcameronpresley Appending to values 74
  75. 75. @pcameronpresley Redoing Keep as Reduce 75
  76. 76. @pcameronpresley 76 What’s the initial value? What should we return if the source is empty? Enumerable<TSource>.Empty()
  77. 77. @pcameronpresley Setting Up Keep via Reduce 77 1. Check if the item meets the criteria 2. If the item meets, append to values %%
  78. 78. @pcameronpresley Checking if an item is valid 78
  79. 79. @pcameronpresley Appending to values 79
  80. 80. @pcameronpresley What Did We Cover? What is LINQ? Fundamentals (Func, Generics, Extension Methods) Implementing Map, Keep, Reduce 80
  81. 81. @pcameronpresley LINQ isn’t magic 81
  82. 82. @pcameronpresley Composition of Funcs, Generics, Extension Methods 82
  83. 83. @pcameronpresley Building bigger programs from smaller programs 83
  84. 84. @pcameronpresley Questions? Cameron@TheSoftwareMentor.com 84

×