on master help sign in
changes the code in caesar.udon into a naked generator
b1b5787Robert
learn/hoon/workbook/caesar.udon+20−32
@@ -33,7 +33,7 @@ demonstrates important principles that more laconic code would not. Save it as
33
33
34```
34```
35!:
35!:
36−:- %say
36+|= [msg=tape key=@ud]
37=+ ^= caesar
37=+ ^= caesar
38 |%
38 |%
39 ++ alpha "abcdefghijklmnopqrstuvwxyz"
39 ++ alpha "abcdefghijklmnopqrstuvwxyz"
@@ -76,8 +76,6 @@ demonstrates important principles that more laconic code would not. Save it as
76 =+ s=(trim (mod n length) m)
76 =+ s=(trim (mod n length) m)
77 (weld q.s p.s)
77 (weld q.s p.s)
78 --
78 --
79−|= [* [msg=tape key=@ud ~] ~]
80−:- %noun
81=. msg (cass msg)
79=. msg (cass msg)
82:- (shift.caesar msg key)
80:- (shift.caesar msg key)
83(unshift.caesar msg key)
81(unshift.caesar msg key)
@@ -91,24 +89,23 @@ that has been shifted left. It also converts any uppercase input into lowercase.
91Try it out in the Dojo:
89Try it out in the Dojo:
92
90
93```
91```
94−> +caesar "test" 2
92+> +caesar ["test" 2]
95["vguv" "rcqr"]
93["vguv" "rcqr"]
96
94
97−> +caesar "test" 26
95+> +caesar ["test" 26]
98["test" "test"]
96["test" "test"]
99
97
100−> +caesar "test" 28
98+> +caesar ["test" 28]
101["vguv" "rcqr"]
99["vguv" "rcqr"]
102
100
103−> +caesar "test" 104
101+> +caesar ["test" 104]
104["test" "test"]
102["test" "test"]
105
103
106−> +caesar "tESt" 2
104+> +caesar ["tESt" 2]
107["vguv" "rcqr"]
105["vguv" "rcqr"]
108
106
109−> +caesar "test!" 2
107+> +caesar ["test!" 2]
110nest-fail
108nest-fail
111−
112```
109```
113
110
114### Examining Our Code
111### Examining Our Code
@@ -120,7 +117,7 @@ itself before reading the explanation.
120
117
121```
118```
122!:
119!:
123−:- %say
120+|= [msg=tape key=@ud]
124=+ ^= caesar
121=+ ^= caesar
125 |%
122 |%
126```
123```
@@ -128,34 +125,23 @@ itself before reading the explanation.
128The `!:` in the first line of the above code enables a full stack trace in the
125The `!:` in the first line of the above code enables a full stack trace in the
129event of an error.
126event of an error.
130
127
131−`:- %say` begins to construct a cell with `%say` as the head, and the code that
128+`|= [msg=tape key=@ud]` creates a gate that takes a cell. The head of this cell
132−follows as the tail. This causes the Arvo to interpret the file as a
129+is a `tape`, which is a string type that's a list of `cord`s. Tapes are represented
133−[`%say` generator](/docs/tutorials/generators).
130+as text surrounded by double-quotes, such as this: `"a tape"`. We give this input tape the face `msg`.
131+The tail of our cell is a `@ud` -- an unsigned decimal atom -- that we give the face `key`.
134
132
135−`=+` adds a noun the subject, `=^ caesar` names that noun, and `|%` is used
133+`=+` adds a noun to the subject, `=^ caesar` names that noun, and `|%` is used
136to create a core that contains the following arms. This results in having a core
134to create a core that contains the following arms. This results in having a core
137with the face `caesar` that we can call.
135with the face `caesar` that we can call.
138
136
139Now let's look at the rest of the program that is outside of our core:
137Now let's look at the rest of the program that is outside of our core:
140
138
141```
139```
142−|= [* [msg=tape key=@ud ~] ~]
143−:- %noun
144=. msg (cass msg)
140=. msg (cass msg)
145:- (shift.caesar msg key)
141:- (shift.caesar msg key)
146(unshift.caesar msg key)
142(unshift.caesar msg key)
147```
143```
148
144
149−The top line creates a gate with `|=`. The sample of that gate follows the
150−scheme used by `%say` generators: it's a 3-tuple whose parts allow for different
151−kinds of data. We only need non-optional arguments, though, so we only fill in
152−the second part. There we take a `msg` argument and a `key` argument. We use a
153−`*` and a `~` in the first part (Arvo data) and third part (optional arguments),
154−respectively, because we aren't using them here. See the
155−[generator documentation](/docs/tutorials/generators) for more information.
156−
157−`:- %noun` simply labels the output of this program as the `%noun` mark.
158−
159`=. msg (cass msg)` changes the input string `msg` to lowercases. `=.` changes
145`=. msg (cass msg)` changes the input string `msg` to lowercases. `=.` changes
160the leg of the subject to something else. In our case, the leg to be changed is
146the leg of the subject to something else. In our case, the leg to be changed is
161`msg`, and the thing to replace it is `(cass msg)`. `cass` is a standard-library
147`msg`, and the thing to replace it is `(cass msg)`. `cass` is a standard-library
@@ -238,7 +224,7 @@ If the above test finds that its condition to be false, we trigger a recursion
238that constructs our `map`: $(chart (~(put by chart) i.a i.b), a t.a, b t.b)`.
224that constructs our `map`: $(chart (~(put by chart) i.a i.b), a t.a, b t.b)`.
239This code recursively adds an entry in our `map` where the head of the tape `a`
225This code recursively adds an entry in our `map` where the head of the tape `a`
240maps to the value of the head of tape `b` with `~(put by chart)`, our calling
226maps to the value of the head of tape `b` with `~(put by chart)`, our calling
241−of the `put` arm of the `by` map-engien core. The recursion also "consumes"
227+of the `put` arm of the `by` map-engine core. The recursion also "consumes"
242those heads with every iteration by changing `a` and `b` to their tails.
228those heads with every iteration by changing `a` and `b` to their tails.
243
229
244Perhaps now you can understand why this arm is named `zipper`. It's putting two
230Perhaps now you can understand why this arm is named `zipper`. It's putting two
@@ -333,7 +319,7 @@ If we then give our arm Caesar's famous statement, and get our left- and
333right-ciphers.
319right-ciphers.
334
320
335```
321```
336−> +caesar "i came i saw i conquered" 4
322+> +caesar ["i came i saw i conquered" 4]
337["m geqi m wea m gsruyivih" "e ywia e ows e ykjmqanaz"]
323["m geqi m wea m gsruyivih" "e ywia e ows e ykjmqanaz"]
338```
324```
339
325
@@ -341,10 +327,10 @@ Now, to decode, we can put either of our ciphers in with the appropriate key and
341look for the legible result.
327look for the legible result.
342
328
343```
329```
344−> +caesar "m geqi m wea m gsruyivih" 4
330+> +caesar ["m geqi m wea m gsruyivih" 4]
345["q kium q aie q kwvycmzml" "i came i saw i conquered"]
331["q kium q aie q kwvycmzml" "i came i saw i conquered"]
346
332
347−> +caesar "e ywia e ows e ykjmqanaz" 4
333+> +caesar ["e ywia e ows e ykjmqanaz" 4]
348["i came i saw i conquered" "a usew a kso a ugfimwjwv"]
334["i came i saw i conquered" "a usew a kso a ugfimwjwv"]
349```
335```
350
336
@@ -353,7 +339,9 @@ look for the legible result.
353
339
3541. Take the example generator and modify it to add a second layer of shifts.
3401. Take the example generator and modify it to add a second layer of shifts.
355
341
356−2. Extend the example generator to allow for using characters other than a-z.
342+2. Extend the example generator to allow for use of characters other than a-z.
357
343
3583. Build a gate that can take a Caesar shifted `tape` and produce
3443. Build a gate that can take a Caesar shifted `tape` and produce
359possible unshifted `tapes`.
345possible unshifted `tapes`.
346+
347+4. Modify the example generator into a `%say` generator.