on master help sign in
Commented make-deck
commit462be2e4dc03e6aba6cb1aea8ade399ab28e4167 authorDavid Kerschner <dkerschner@hcoop.net> dateTue 18 Jun 2019, 16:51 (7 years ago) parenta3d2d13
1 file changed+21
learn/hoon/hoon-tutorial/libraries.md+21
learn/hoon/hoon-tutorial/libraries.md+21
@@ -92,6 +92,27 @@ We can see this is a gate that takes a single `@ud` and produces a `suit`.
9292
9393The `?+` 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.
9494
95+```
96+++ make-deck
97+ ^- deck
98+ =| mydeck=deck
99+ =/ i 1
100+ |-
101+ ?: (gth i 4)
102+ mydeck
103+ =/ j 1
104+ |-
105+ ?. (lte j 13)
106+ ^$(i +(i))
107+ %= $
108+ j +(j)
109+ mydeck [(num-to-suit i) j]^mydeck
110+ ==
111+```
112+
113+`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.
114+
115+
95116```
96117++ draw
97118 |= [n=@ud d=deck]