5title: Using Generators with Apps
10Up until now we've poked apps directly. This requires the user
11to specify the mark, and it requires the app to accept the
12arguments in a way that's convenient for users to input. This is
13the "plumbing" way to interact with apps. Generators are the
14"porcelain" layer. This is why when you run a command like `+ls` or
15`|merge`, there are no marks in sight.
17We've used generators before, back in [Basic
18Operation](/docs/user/basic). At that point, we just used the generators
19to produce values -- we didn't pipe their results into apps. In
20the dojo cast, the role of a generator is to take a list of
21arguments and produce a value, which is often, though not always,
22piped into an app. Generators are pure, stateless functions, and
23they cannot send moves.
25`+ls` and `+cat` are commonly-used generators that produce a directory
26listing and print out a file, respectively. These are useful in
27themselves, so we generally don't pipe the results into an app.
29Another generator is `+hood/merge`. Try running `+hood/merge`
30with the normal arguments for `|merge`:
33~fintud-macrep:dojo> +hood/merge %home our %they
34[syd=%home her=~fintud-macrep sud=%they gem=%auto]
35~fintud-macrep:dojo> +hood/merge %home our %they, =gem %this
36[syd=%home her=~fintud-macrep sud=%they gem=%auto]
39This didn't run any merge, it just constructed the command that,
40if sent to `:hood`, would run it. Note first that, even though
41it's not printed, this value has mark `kiln-merge`. Also, the
42merge strategy `gem=%auto` was added automatically. Optional
43arguments are straightforward with generators.
45In general, generators are prepended by either `+` or `|`. The
46general form is `+generator`, but often generators are created
47specifically to work with a single app, they're usually placed in
48a subdirectory of gen and run with `|`. Thus, if `my-generator`
49generator is made for use with `:my-app`, then `my-generator` is
50put in `/gen/my-app/my-generator.hoon`, and can be run directly
51with `+my-app/my-generator <args>`. If you want to pipe the
52result into `:my-app`, then run `:my-app +my-app/my-generator
53<args>`. Because this pattern is so common, this can be
54abbreviated to `:my-app|my-generator <args>`. Because most
55built-in commands are generators for the `:hood` app,
56`:hood|generator <args>` can be shortened to `|generator <args>`.
58Let's write a generator for a modified version of `:examples-pong` from
59the chapter on [Network Messages](/docs/programming/system/network). Recall that `:examples-pong`
60takes an urbit address, which is of mark `urbit`, and sends that
61urbit the message `'howdy'`. First--without a generator--let's
62make `:examples-ping` that does the same, except that it lets the user
63optionally specify the message as well.
65We'll need a new mark for our arguments. Let's call it
66`examples-ping-message`.
68<blockquote class="blockquote">
69For app-specific marks, it's good style to prefix the name of
70the mark with the name of the app. Since many apps have
71several such marks, subdirectories in `/mar` are rendered as
72`-`, so that `ping-message` is written in
73(/mar/examples/ping/message.hoon).
77:: Up-ness monitor. Accepts atom url, 'on', or 'off'
79:::: /hoon/up/examples/app
85 $% {$hiss wire $~ $httr {$purl p/purl}}
89 $% {$on $~} :: enable polling('on')
90 {$off $~} :: disable polling('off)
91 {$target p/cord} :: set poll target('http://...')
94|_ {hid/bowl on/_| in-progress/_| target/@t}
96 |= url-or-command/@t ^- (quip move +>)
98 ?: ?=($on url-or-command) [%on ~]
99 ?: ?=($off url-or-command) [%off ~]
100 [%target url-or-command]
102 $target [~ +>.$(target p.act)]
105 :- ?: |(on in-progress) ~
106 [ost.hid %hiss /request ~ %httr %purl (need (epur target))]~
107 +>.$(on &, in-progress &)
112:: ^- {(list move) _+>.$}
116:: :_ +>.$(on &, in-progress &)
117:: ?: |(on in-progress)
119:: [ost.hid %them /request ~ (need (epur target)) %get ~ ~]~
120:: [~ +>.$(target url)]
122 |= {wir/wire code/@ud headers/mess body/(unit octs)}
124 ^- {(list move) _+>.$}
125 ?: &((gte code 200) (lth code 300))
126 ~& [%all-is-well code]
128 [ost.hid %wait /timer (add ~s10 now.hid)]~
129 ~& [%we-have-a-problem code]
130 ~& [%headers headers]
133 [ost.hid %wait /timer (add ~s10 now.hid)]~
135 |= {wir/wire $~} ^- (quip move +>)
138 [ost.hid %hiss /request ~ %httr %purl (need (epur target))]~
139 [~ +>.$(in-progress |)]
141++ prep ~& target _`. ::
145The app can easily be modified to use this (`/app/examples/ping.hoon`):
149:::: /hoon/ping/examples/app
153 ++ move {bone term wire *}
158++ poke-examples-ping-message
159 |= {to/@p message/@t}
161 ^- {(list move) _+>.$}
163 :~ `move`[ost %poke /sending [to dap] %atom message]
170 ^- {(list move) _+>.$}
172 ~& [%receiving (@t arg)]
181Now we can run this with:
184~fintud-macrep:dojo> |start %examples-ping
186~fintud-macrep:dojo> :examples-ping &examples-ping-message [~sampel-sipnym 'heyya']
190And on `~sampel-sipnym`, assuming it's running `:examples-ping` as well,
191we see `[%receiving 'heyya']`.
193This is an annoying way to invoke the app, though. The `[]` are
194mandatory, and optional arguments are hard. Let's make a
195generator, `+examples-send` to make everything nicer. Since it's specific
196to `:examples-ping`, let's put it in `/gen/examples/ping/send.hoon`:
200|= {^ {{to/@p message/?($~ {text/@t $~})} $~}
201[%examples-ping-message to ?~(message 'howdy' text.message)]
204A couple of new things here. Firstly, `message/?($~ {text/@t $~})`
205should be read as "the message is either null or a pair of text
206and null". Generator argument lists are always null-terminated,
207which makes it convenient to accept lists in tail position (which
208are particularly annoying without generators). `?(a b)` is the
209irregular form of `$?(a b)`, which is a union type. Thus, `?(a
210b)` means the type of anything that's in either `a` or `b`.
211Thus, in our case, `?(~ [text=@t ~])` is either null or a pair of
214Secondly, `?~(a b c)` is a rune which means "if the `a` is null,
215do `b`, else `c`". It is roughly equivalent to `?:(=(~ a) b c)`.
217<blockquote class="blockquote">
218`?~(a b c)` is actually equivalent to `?:=(?=(~ a) b c)`,
219which, although identical at run time, is subtly different at
220compile time. Specifically, using `?=` rather than `=` means
221that we're checking whether `a` is in the *type* of `~`, and so
222the compiler knows that in the `b` case `a` is null, and in the
223`c` case `a` is not null. Since `=` is purely a runtime value
224check with no type implications, the compiler doesn't gain any
227At any rate, this is the reason why we can refer to
228`text.message` in the `c` clause. The compiler knows that
229`message` is not null, so it must have `text` within it. If
230you used `?:(=(~ message) 'howdy' text.message)` the compiler
231would complain that it doesn't know whether `message` has
235This is run as follows:
238~fintud-macrep:dojo> :examples-ping|send ~sampel-sipnym
240~fintud-macrep:dojo> :examples-ping|send ~sampel-sipnym 'how do you do'
244Which causes `~sampel-sipnym` to print `[%receiving 'howdy']` and
245`[%receiving 'how do you do']`.
249- Create a generator for `:examples-sum` from [State](state) so that
250 you can run `:examples-sum|add 5` to add numbers to it.
252- Create a generator for `:examples-click` from [Web Apps](web-apps) so
253 that you can run `:examplesclick|poke` to poke it.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list