on master help sign in
Commented make-deck
6705a7bDavid Kerschner
learn/hoon/hoon-tutorial/libraries.md+21
@@ -93,6 +93,27 @@ We can see this is a gate that takes a single `@ud` and produces a `suit`.
93
93
94The `?+` rune is the rune to switch against a value with a default. The default here is to crash with `!!`. Then we have the four options of 1 through 4, based on the value of `val`, which each result in a different suit.
94The `?+` rune is the rune to switch against a value with a default. The default here is to crash with `!!`. Then we have the four options of 1 through 4, based on the value of `val`, which each result in a different suit.
95
95
96+```
97+++ make-deck
98+ ^- deck
99+ =| mydeck=deck
100+ =/ i 1
101+ |-
102+ ?: (gth i 4)
103+ mydeck
104+ =/ j 1
105+ |-
106+ ?. (lte j 13)
107+ ^$(i +(i))
108+ %= $
109+ j +(j)
110+ mydeck [(num-to-suit i) j]^mydeck
111+ ==
112+```
113+
114+`num-to-suit` is used in the `make-deck` arm. This arm should be quite readable to you at this point. Here we simply have two loops where we use counters to build up the full set of 52 cards. Once we have reached the point where `j` is greater than 13, we're going to jump back out the outer loop and increment `i`. `?.` may be an unfamiliar rune but it is simply the inverted version of `?:` so the first branch is actually the no branch and the second is the yes branch. Again this is done to keep the heaviest branch at the bottom, though this could also be achieved in this case by reversing the conditional.
115+
116+
96```
117```
97++ draw
118++ draw
98 |= [n=@ud d=deck]
119 |= [n=@ud d=deck]