Commented make-deck
commit6705a7bdaa118b2be8f44156d48248a7d988b20c
authorDavid Kerschner <dkerschner@hcoop.net>
dateTue 18 Jun 2019, 16:51 (7 years ago)
parent465c169
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`.
9393
9494The `?+` 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.
9595
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+
96117```
97118++ draw
98119 |= [n=@ud d=deck]