Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
What to Upload to SlideShare
Next
Download to read offline and view in fullscreen.

Share

Taking a Gamble with Functional Domain Modeling

Download to read offline

Presentation for Functional Domain Modeling where I cover how to identify/implement the domain for Blackjack. From there, we take a look at implementing various workflows by exploring the Maybe type!

Related Books

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Taking a Gamble with Functional Domain Modeling

  1. 1. @pcameronpresley Taking a Gamble With Functional Domain Modeling Cameron Presley Cameron@TheSoftwareMentor.com
  2. 2. @pcameronpresley https://blog.TheSoftwareMentor.com/Presentations No Need To Take Notes!
  3. 3. @pcameronpresley 3
  4. 4. @pcameronpresley What Are We Covering? Introduction to Blackjack Defining the Domain Implementing the Domain Implementing common workflows 4
  5. 5. 5
  6. 6. @pcameronpresley Overview Game played with a standard deck of cards Objective is to get closest to 21 points without going over Involves one or more players playing against the dealer 6
  7. 7. @pcameronpresley Set Up Each player is dealt two cards face-up to form their hand Dealer is dealt two cards (one face-up) to form their hand 7
  8. 8. @pcameronpresley Game Play On their turn, a player can take an action Once all players have taken their turn, dealer takes their turn After dealer completes their turn, each player compares their hand with the dealer 8
  9. 9. @pcameronpresley Player Turn Players can either “Hit” or “Stand” ◦ Stand – Ends the turn, staying in ◦ Hit – Draw a card If their hand goes over 21, they “bust” ending their turn and leaving the round 9
  10. 10. @pcameronpresley Scoring Aces can be worth 1 or 11 points Face cards are worth 10 points Number cards are worth that many points If a player or dealer is dealt a hand worth 21 points (an Ace and a 10 point card), they have a Blackjack, winning automatically 10
  11. 11. 11
  12. 12. 12
  13. 13. @pcameronpresley What are the nouns? Game played with a standard deck of cards Objective is to get closest to 21 points without going over Involves one or more players playing against the dealer 13
  14. 14. @pcameronpresley 14
  15. 15. @pcameronpresley Each player is dealt two cards face-up to form their hand Dealer is dealt two cards (one face-up) to form their hand 15
  16. 16. @pcameronpresley 16
  17. 17. @pcameronpresley On their turn, a player can take an action Once all players have taken their turn, dealer takes their turn After dealer completes their turn, each player compares their hand with the dealer 17
  18. 18. @pcameronpresley 18
  19. 19. @pcameronpresley Players can either “Hit” or “Stand” ◦ Stand – Ends the turn, staying in ◦ Hit – Draw a card If their hand goes over 21, they “bust” ending their turn and leaving the round 19
  20. 20. @pcameronpresley 20
  21. 21. @pcameronpresley Building Up the Dependency Chart 21
  22. 22. @pcameronpresley Determining Dependencies using “has-a” Game has a Deck, a Dealer, and Players Deck has one or more Cards Dealer has a Hand Player has a Hand Hand has one or more Cards Card has a Rank and Suit 22
  23. 23. @pcameronpresley 23
  24. 24. @pcameronpresley 24
  25. 25. @pcameronpresley 25
  26. 26. 26
  27. 27. @pcameronpresley 27
  28. 28. @pcameronpresley Rank Represents one of 13 possible values Rank can be an Ace OR a Two OR a Three… How to Model? 28
  29. 29. @pcameronpresley Introducing The Sum Type! Type that can be one of a finite number of choices Each choice is a different constructor When a type can be A or B or C 29
  30. 30. @pcameronpresley Rank 30
  31. 31. @pcameronpresley 31
  32. 32. @pcameronpresley Suit Represents one of four possible values Suit can be Hearts or Clubs or Spades or Diamonds How can we model this? 32
  33. 33. @pcameronpresley Suit 33
  34. 34. @pcameronpresley 34
  35. 35. @pcameronpresley Card Represents the combination of any Rank and Suit 13 Ranks x 4 Suits = 52 possibilities How can we model combinations? 35
  36. 36. @pcameronpresley Introducing the Product Type! Type that specifies the combination of other types It’s the product of the possible values for all types it combines 36
  37. 37. @pcameronpresley Card 37
  38. 38. @pcameronpresley 38
  39. 39. @pcameronpresley Deck Represents the Cards to be used in the game Players and Dealers will be drawing from this during the Game How to model? 39
  40. 40. @pcameronpresley Introducing the Type Alias! 40 Any code that can work on a Card List will work on a Deck
  41. 41. @pcameronpresley 41
  42. 42. @pcameronpresley Hand Represents one or more Cards that a Player or Dealer has How to model this? Leveraging a Type alias! 42
  43. 43. @pcameronpresley 43 Any issue with Hand and Deck being interchangeable?
  44. 44. @pcameronpresley Making Deck Different 44 In order to create this type, a Card list must be specified Deck != Card list
  45. 45. @pcameronpresley 45 [] [] []
  46. 46. @pcameronpresley 46
  47. 47. @pcameronpresley Player Represents someone playing the game Has a Hand and an ID How to model? 47
  48. 48. @pcameronpresley Player 48
  49. 49. @pcameronpresley 49
  50. 50. @pcameronpresley Dealer Represents who every player is competing against Has a Hand How to model? 50
  51. 51. @pcameronpresley Dealer 51
  52. 52. @pcameronpresley 52
  53. 53. @pcameronpresley Game It’s what’s being played! Consists of a Deck, a Dealer, and 1 or more Players How to Model? 53
  54. 54. @pcameronpresley Game 54
  55. 55. @pcameronpresley 55
  56. 56. @pcameronpresley Status Represents the Player or Dealer during the game Can be one of 3 possible values - Blackjack, Busted, Stayed How to model? 56
  57. 57. @pcameronpresley Status 57 To construct Stayed or Busted, an integer is needed No dependencies to construct the Blackjack type
  58. 58. @pcameronpresley 58
  59. 59. @pcameronpresley Points Represents how much a Card is worth Ace = 1 or 11 points Face Cards = 10 points Number Cards = That number of points How to model? 59
  60. 60. @pcameronpresley Points 60 To construct the Hard type, need an int To construct the Soft type, need a tuple of int and int
  61. 61. @pcameronpresley 61
  62. 62. @pcameronpresley Action Represents what a Player can do during the Game Can either be Hit or Stay How to model? 62
  63. 63. @pcameronpresley Action 63
  64. 64. @pcameronpresley 64
  65. 65. @pcameronpresley Domain Defined 65
  66. 66. 66
  67. 67. 67
  68. 68. @pcameronpresley 68
  69. 69. 69
  70. 70. @pcameronpresley Scoring a Hand How Would You Score a Hand? Calculate how many points each card is worth Sum them up! 70
  71. 71. @pcameronpresley Scoring Rules Aces can be worth 1 or 11 points Face cards are worth 10 points Number cards are worth that many points 71
  72. 72. @pcameronpresley 72
  73. 73. @pcameronpresley How to add points? 73
  74. 74. @pcameronpresley Scoring a Hand 74
  75. 75. 75
  76. 76. @pcameronpresley Drawing A Card! 76 What do we do if the deck is empty?
  77. 77. @pcameronpresley Introducing the Maybe Type! Allows us to model the absence of a value Compile time checked Forces us to do the right thing 77
  78. 78. @pcameronpresley 78 Most languages have this construct In F#, it’s called Option
  79. 79. @pcameronpresley 79 By using Option, we’re signaling that this function may return a value and that callers will need to deal with the absence of value
  80. 80. @pcameronpresley Let’s Draw Multiple Cards! 80
  81. 81. @pcameronpresley 81 m
  82. 82. @pcameronpresley 82 Where did maybe come from? Where did let! come from? Where did return come from?
  83. 83. @pcameronpresley 83
  84. 84. 84
  85. 85. @pcameronpresley What Did We Cover? Introduction to Blackjack Defining the Domain Implementing the Domain Implementing common workflows 85
  86. 86. @pcameronpresley Resources ◦ Thinking Functionally (series) by Scott Wlaschin ◦ Functional Architecture : a definition by Mark Seemann ◦ Functional Architecture Is Ports and Adapters by Mark Seemann ◦ Algebraic Data Types Aren't Numbers on Steroids by Mark Seemann ◦ Domain Modeling Made Functional by Scott Wlaschin
  87. 87. @pcameronpresley 88 Thanks! Any questions? ◦ Cameron@TheSoftwareMentor.com

Presentation for Functional Domain Modeling where I cover how to identify/implement the domain for Blackjack. From there, we take a look at implementing various workflows by exploring the Maybe type!

Views

Total views

111

On Slideshare

0

From embeds

0

Number of embeds

0

Actions

Downloads

1

Shares

0

Comments

0

Likes

0

×