on master help sign in
rogue links in arvo/generators.md
8b620d6Tobias Merkle
docs/arvo/generators.md+139−153
@@ -1,192 +1,178 @@
1---
1---
2−navhome: /docs
2+navhome: '/docs'
3−next: true
3+next: True
4sort: 8
4sort: 8
5title: Using Generators with Apps
5title: Using Generators with Apps
6---
6---
7
7
8# Generators
8# Generators
9
9
10−Up until now we've poked apps directly. This requires the user
10+Up until now we've poked apps directly. This requires the user to
11−to specify the mark, and it requires the app to accept the
11+specify the mark, and it requires the app to accept the arguments in a
12−arguments in a way that's convenient for users to input. This is
12+way that's convenient for users to input. This is the "plumbing" way to
13−the "plumbing" way to interact with apps. Generators are the
13+interact with apps. Generators are the "porcelain" layer. This is why
14−"porcelain" layer. This is why when you run a command like `+ls` or
15−`|merge`, there are no marks in sight.
14+when you run a command like `+ls` or `|merge`, there are no marks in
15+sight.
16
16
17We've used generators before, back in [Basic
17We've used generators before, back in [Basic
18−Operation](/docs/using/admin). At that point, we just used the generators
18+Operation](/docs/using/admin). At that point, we just used the
19−to produce values -- we didn't pipe their results into apps. In
19+generators to produce values -- we didn't pipe their results into apps.
20−the dojo cast, the role of a generator is to take a list of
20+In the dojo cast, the role of a generator is to take a list of arguments
21−arguments and produce a value, which is often, though not always,
21+and produce a value, which is often, though not always, piped into an
22−piped into an app. Generators are pure, stateless functions, and
22+app. Generators are pure, stateless functions, and they cannot send
23−they cannot send moves.
23+moves.
24
24
25`+ls` and `+cat` are commonly-used generators that produce a directory
25`+ls` and `+cat` are commonly-used generators that produce a directory
26−listing and print out a file, respectively. These are useful in
26+listing and print out a file, respectively. These are useful in
27themselves, so we generally don't pipe the results into an app.
27themselves, so we generally don't pipe the results into an app.
28
28
29−Another generator is `+hood/merge`. Try running `+hood/merge`
29+Another generator is `+hood/merge`. Try running `+hood/merge` with the
30−with the normal arguments for `|merge`:
30+normal arguments for `|merge`:
31−
31+
32−```
33−~fintud-macrep:dojo> +hood/merge %home our %they
32+ ~fintud-macrep:dojo> +hood/merge %home our %they
34−[syd=%home her=~fintud-macrep sud=%they gem=%auto]
33+ [syd=%home her=~fintud-macrep sud=%they gem=%auto]
35−~fintud-macrep:dojo> +hood/merge %home our %they, =gem %this
34+ ~fintud-macrep:dojo> +hood/merge %home our %they, =gem %this
36−[syd=%home her=~fintud-macrep sud=%they gem=%auto]
35+ [syd=%home her=~fintud-macrep sud=%they gem=%auto]
37−```
36+
38−
39−This didn't run any merge, it just constructed the command that,
37+This didn't run any merge, it just constructed the command that, if sent
40−if sent to `:hood`, would run it. Note first that, even though
38+to `:hood`, would run it. Note first that, even though it's not printed,
41−it's not printed, this value has mark `kiln-merge`. Also, the
39+this value has mark `kiln-merge`. Also, the merge strategy `gem=%auto`
42−merge strategy `gem=%auto` was added automatically. Optional
43−arguments are straightforward with generators.
40+was added automatically. Optional arguments are straightforward with
44−
41+generators.
42+
45−In general, generators are prepended by either `+` or `|`. The
43+In general, generators are prepended by either `+` or `|`. The general
46−general form is `+generator`, but often generators are created
44+form is `+generator`, but often generators are created specifically to
47−specifically to work with a single app, they're usually placed in
45+work with a single app, they're usually placed in a subdirectory of gen
48−a subdirectory of gen and run with `|`. Thus, if `my-generator`
46+and run with `|`. Thus, if `my-generator` generator is made for use with
49−generator is made for use with `:my-app`, then `my-generator` is
47+`:my-app`, then `my-generator` is put in
50−put in `/gen/my-app/my-generator.hoon`, and can be run directly
48+`/gen/my-app/my-generator.hoon`, and can be run directly with
51−with `+my-app/my-generator <args>`. If you want to pipe the
49+`+my-app/my-generator <args>`. If you want to pipe the result into
52−result into `:my-app`, then run `:my-app +my-app/my-generator
50+`:my-app`, then run `:my-app +my-app/my-generator <args>`. Because this
53−<args>`. Because this pattern is so common, this can be
51+pattern is so common, this can be abbreviated to
54−abbreviated to `:my-app|my-generator <args>`. Because most
52+`:my-app|my-generator <args>`. Because most built-in commands are
55−built-in commands are generators for the `:hood` app,
53+generators for the `:hood` app, `:hood|generator <args>` can be
56−`:hood|generator <args>` can be shortened to `|generator <args>`.
54+shortened to `|generator <args>`.
57
55
58Let's write a generator for a modified version of `:examples-pong` from
56Let's write a generator for a modified version of `:examples-pong` from
59−the chapter on [Network Messages](/docs/programming/system/network). Recall that `:examples-pong`
57+the chapter on [Network Messages](/docs/arvo/system/network). Recall
60−takes an urbit address, which is of mark `urbit`, and sends that
58+that `:examples-pong` takes an urbit address, which is of mark `urbit`,
61−urbit the message `'howdy'`. First--without a generator--let's
59+and sends that urbit the message `'howdy'`. First--without a
62−make `:examples-ping` that does the same, except that it lets the user
60+generator--let's make `:examples-ping` that does the same, except that
63−optionally specify the message as well.
61+it lets the user optionally specify the message as well.
64
62
65−We'll need a new mark for our arguments. Let's call it
63+We'll need a new mark for our arguments. Let's call it
66`examples-ping-message`.
64`examples-ping-message`.
67
65
68−> For app-specific marks, it's good style to prefix the name of
66+> For app-specific marks, it's good style to prefix the name of the mark
69−> the mark with the name of the app. Since many apps have
67+> with the name of the app. Since many apps have several such marks,
70−> several such marks, subdirectories in `/mar` are rendered as
68+> subdirectories in `/mar` are rendered as `-`, so that `ping-message`
71−> `-`, so that `ping-message` is written in
72−> (/mar/examples/ping/message.hoon).
69+> is written in (/mar/examples/ping/message.hoon).
73−
70+
74−```
75−::
71+ ::
76−:::: /hoon/message/ping/examples/app
72+ :::: /hoon/message/ping/examples/app
77− ::
73+ ::
78−/? 314
74+ /? 314
79−|_ {to/@p message/@t}
75+ |_ {to/@p message/@t}
80−++ grab
76+ ++ grab
81− |%
77+ |%
82− ++ noun {@p @t}
78+ ++ noun {@p @t}
83− --
79+ --
84−--
80+ --
85−```
86
81
87The app can easily be modified to use this (`/app/examples/ping.hoon`):
82The app can easily be modified to use this (`/app/examples/ping.hoon`):
88
83
89−```
90−:: Allows one ship to ping another with a string of text
84+ :: Allows one ship to ping another with a string of text
91−::
85+ ::
92−:::: /hoon/ping/examples/app
86+ :::: /hoon/ping/examples/app
93− ::
87+ ::
94−/? 314
88+ /? 314
95−|%
89+ |%
96− ++ move {bone term wire *}
90+ ++ move {bone term wire *}
97−--
91+ --
98−!:
92+ !:
99−|_ {bowl state/$~}
93+ |_ {bowl state/$~}
100−::
94+ ::
101−++ poke-examples-ping-message
95+ ++ poke-examples-ping-message
102− |= {to/@p message/@t}
96+ |= {to/@p message/@t}
103− ~& 'sent'
97+ ~& 'sent'
104− ^- {(list move) _+>.$}
98+ ^- {(list move) _+>.$}
105− :- ^- (list move)
99+ :- ^- (list move)
106− :~ `move`[ost %poke /sending [to dap] %atom message]
100+ :~ `move`[ost %poke /sending [to dap] %atom message]
107− ==
101+ ==
108− +>.$
102+ +>.$
109−::
103+ ::
110−++ poke-atom
104+ ++ poke-atom
111− |= arg/@
105+ |= arg/@
112− ~& 'received'
106+ ~& 'received'
113− ^- {(list move) _+>.$}
107+ ^- {(list move) _+>.$}
114− ::
108+ ::
115− ~& [%receiving (@t arg)]
109+ ~& [%receiving (@t arg)]
116− [~ +>.$]
110+ [~ +>.$]
117−::
111+ ::
118−++ coup |=(* `+>)
112+ ++ coup |=(* `+>)
119−--
113+ --
120−```
121
114
122Now we can run this with:
115Now we can run this with:
123
116
124−```
125−~fintud-macrep:dojo> |start %examples-ping
117+ ~fintud-macrep:dojo> |start %examples-ping
126−>=
118+ >=
127−~fintud-macrep:dojo> :examples-ping &examples-ping-message [~sampel-sipnym 'heyya']
119+ ~fintud-macrep:dojo> :examples-ping &examples-ping-message [~sampel-sipnym 'heyya']
128−>=
120+ >=
129−```
130
121
131And on `~sampel-sipnym`, assuming it's running `:examples-ping` as well,
122And on `~sampel-sipnym`, assuming it's running `:examples-ping` as well,
132we see `[%receiving 'heyya']`.
123we see `[%receiving 'heyya']`.
133
124
134−This is an annoying way to invoke the app, though. The `[]` are
125+This is an annoying way to invoke the app, though. The `[]` are
135−mandatory, and optional arguments are hard. Let's make a
126+mandatory, and optional arguments are hard. Let's make a generator,
136−generator, `+examples-send` to make everything nicer. Since it's specific
127+`+examples-send` to make everything nicer. Since it's specific to
137−to `:examples-ping`, let's put it in `/gen/examples/ping/send.hoon`:
128+`:examples-ping`, let's put it in `/gen/examples/ping/send.hoon`:
138−
129+
139−```
140−:- %say
130+ :- %say
141−|= {^ {to/@p message/?($~ {text/@t $~})} $~}
131+ |= {^ {to/@p message/?($~ {text/@t $~})} $~}
142−[%examples-ping-message to ?~(message 'howdy' text.message)]
132+ [%examples-ping-message to ?~(message 'howdy' text.message)]
143−```
133+
144−
145−A couple of new things here. Firstly, `message/?($~ {text/@t $~})`
134+A couple of new things here. Firstly, `message/?($~ {text/@t $~})`
146−should be read as "the message is either null or a pair of text
135+should be read as "the message is either null or a pair of text and
147−and null". Generator argument lists are always null-terminated,
136+null". Generator argument lists are always null-terminated, which makes
148−which makes it convenient to accept lists in tail position (which
137+it convenient to accept lists in tail position (which are particularly
149−are particularly annoying without generators). `?(a b)` is the
138+annoying without generators). `?(a b)` is the irregular form of
150−irregular form of `$?(a b)`, which is a union type. Thus, `?(a
139+`$?(a b)`, which is a union type. Thus, `?(a b)` means the type of
151−b)` means the type of anything that's in either `a` or `b`.
140+anything that's in either `a` or `b`. Thus, in our case,
152−Thus, in our case, `?(~ [text=@t ~])` is either null or a pair of
141+`?(~ [text=@t ~])` is either null or a pair of text and null.
153−text and null.
142+
154−
155−Secondly, `?~(a b c)` is a rune which means "if the `a` is null,
143+Secondly, `?~(a b c)` is a rune which means "if the `a` is null, do `b`,
156−do `b`, else `c`". It is roughly equivalent to `?:(=(~ a) b c)`.
144+else `c`". It is roughly equivalent to `?:(=(~ a) b c)`.
157−
145+
158−> `?~(a b c)` is actually equivalent to `?:=(?=(~ a) b c)`,
146+> `?~(a b c)` is actually equivalent to `?:=(?=(~ a) b c)`, which,
159−> which, although identical at run time, is subtly different at
147+> although identical at run time, is subtly different at compile time.
160−> compile time. Specifically, using `?=` rather than `=` means
148+> Specifically, using `?=` rather than `=` means that we're checking
161−> that we're checking whether `a` is in the *type* of `~`, and so
149+> whether `a` is in the *type* of `~`, and so the compiler knows that in
162−> the compiler knows that in the `b` case `a` is null, and in the
150+> the `b` case `a` is null, and in the `c` case `a` is not null. Since
163−> `c` case `a` is not null. Since `=` is purely a runtime value
164−> check with no type implications, the compiler doesn't gain any
151+> `=` is purely a runtime value check with no type implications, the
165−> information.
152+> compiler doesn't gain any information.
166>
153>
167−> At any rate, this is the reason why we can refer to
154+> At any rate, this is the reason why we can refer to `text.message` in
168−> `text.message` in the `c` clause. The compiler knows that
155+> the `c` clause. The compiler knows that `message` is not null, so it
169−> `message` is not null, so it must have `text` within it. If
156+> must have `text` within it. If you used
170−> you used `?:(=(~ message) 'howdy' text.message)` the compiler
157+> `?:(=(~ message) 'howdy' text.message)` the compiler would complain
171−> would complain that it doesn't know whether `message` has
158+> that it doesn't know whether `message` has `text` within it.
172−> `text` within it.
173
159
174This is run as follows:
160This is run as follows:
175
161
176−```
177−~fintud-macrep:dojo> :examples-ping|send ~sampel-sipnym
162+ ~fintud-macrep:dojo> :examples-ping|send ~sampel-sipnym
178−>=
163+ >=
179−~fintud-macrep:dojo> :examples-ping|send ~sampel-sipnym 'how do you do'
164+ ~fintud-macrep:dojo> :examples-ping|send ~sampel-sipnym 'how do you do'
180−>=
165+ >=
181−```
182
166
183Which causes `~sampel-sipnym` to print `[%receiving 'howdy']` and
167Which causes `~sampel-sipnym` to print `[%receiving 'howdy']` and
184`[%receiving 'how do you do']`.
168`[%receiving 'how do you do']`.
185
169
186**Exercises**:
170**Exercises**:
187
171
188−- Create a generator for `:examples-sum` from [State](state) so that
172+- Create a generator for `:examples-sum` from
173+ [State](/docs/arvo/state) so that you can run `:examples-sum|add 5`
189− you can run `:examples-sum|add 5` to add numbers to it.
174+ to add numbers to it.
190
175
191−- Create a generator for `:examples-click` from [Web Apps](web-apps) so
176+- Create a generator for `:examples-click` from [Web
192− that you can run `:examplesclick|poke` to poke it.
177+ Apps](/docs/arvo/web-apps) so that you can run
178+ `:examples-click|poke` to poke it.