10Generators are the most straightforward way to write programs for Urbit. They
11are used for doing computations that do not require persistence: they take an
12input and produce an output, then disappear. Generators might make sense for
13listing directory contents, or running unit tests, or fetching the contents of a
16There are four kinds of generators: naked, `%say`, `%get`, and `%ask`.
20A naked generator is simply a `gate`; that is, it is an anonymous function that
21takes a `sample` (argument) and produces a noun. All you need to do is write a
22`gate` and put it into a file in the `/gen` directory. Let's take a look at a
30This generator takes one argument of any noun and produces it without any
31changes. If we put this into a file named `echo.hoon` in the `/gen` directory,
32we can run it from the [Dojo](../shell):
39This command just passes in 42 and gets 42 back. But what about when we pass in
47We just get a pile of numbers back in the form of a raw `noun`. The numbers that
48compose that noun are called `atoms`, and how they are interpreted is a matter
49of what `aura` is being applied to them. For the purposes of this lesson, an
50`aura` tells the pretty-printer how to display an `atom`, but keep in mind that
51an `aura` is type metadata and does other things, too.
53In the example above, we didn't specify an `aura`, leaving the printer to fend
54for itself. `"asdf"` is a `tape`, a type that is simply a `list` of `cords`. A
55`cord` is itself an atom represented as a string of UTF-8 characters. When used
56as part of a `tape`, the `cord` is only a single character. So each atom in the
57`[97 115 100 102 0]` output corresponds to a component of the `tape`: 97 is
58`a`, 115 is `s`, 100 is `d`, 102 is `f`, and `0` is the "null" that every list
61We can tell the Dojo to cast -- apply a specific type to -- the output of our
62generator to see something more familiar:
69Now let's create a generator with two arguments instead of one. Save the code
70below as `add.hoon` in the `/gen` directory.
77Now, run the generator:
84You may notice our generator takes a cell containing two `@ud`. This is
85actually one of the limitations of naked generators. We can only pass one
86argument. We can get around this, of course, by passing a cell, but it's less
87than ideal. Also, a naked generator cannot be called without an argument.
88This brings us to `%say` generators.
92We use `%say` generators when we want to provide something else in Arvo, the
93Urbit operating system, with metadata about the generator's output. This is
94useful when a generator is needed to pipe data to another program, a frequent
97To that end, `%say` generators use `mark`s to make it clear, to other Arvo
98computations, exactly what kind of data their output is. A `mark` is akin
99to a MIME type on the Arvo level. A `mark` describes the data in
100some way, indicating that it's an `%atom`, or that it's a standard such as
101`%json`, or even that it's an application-specific data structure like
102`%talk-command`. `mark`s are not specific to `%say` generators; whenever data
103moves between programs in Arvo, that data is marked.
105So, more formally, a `%say` generator is a `cell`. The head of that cell is the
106`%say` tag, and the tail is a `gate` that produces a `cask` -- a pair of the
107output data and the `mark` describing that data.
109Below is an example of a `%say` generator. Save it to `add.hoon` in the `/gen`
119Now run the generator as below:
126Notice that we used no argument, something that is possible with `%say`
127generators but impossible with naked generators. We'll explain that in a moment.
128For now, let's focus on the code that is necessary to make something a `%say`
135Recall that the rune `:-` produces a cell, with the first following expression
136as its head and the second following expression as its tail.
138The expression above creates a cell with `%say` as the head. The tail is
139the `|= *` expression on the line that follows.
147`|= *` constructs a gate that takes a noun. This gate will itself produce a
148`cask`, which is cell formed by the prepending `:-`. The head of that `cask` is
149`%noun` and the tail is the rest of the program, `(add 40 2)`. The tail of the
150`cask` will be our actual data produced by the body of the program: in this
151case, just adding 40 and 2 together.
153#### `%say` generators with arguments
155We can modify the boilerplate code to allow arguments to be passed into a `%say`
156generator, but in a way that gives us more power than we would have if we just
157used a naked generator.
159Naked generators are limited because they have no way of accessing data that
160exists in Arvo, such as the date and time or pieces of fresh entropy. In `%say`
161generators, however, we can access that kind of subject by identifying them
162in the gate's sample, which we only specified as `*` in the previous few
163examples. But we can do more with `%say` generators if we do more with that
164sample. Any valid sample will follow this 3-tuple scheme:
166`[[now, eny, beak] [list of unnamed arguments] [list of named arguments]]`
168This entire structure is a noun, which is why `*` is a valid sample if we
169wish to not use any of the information here in a generator. But let's look at
170each of these three elements, piece by piece.
172The *first part* of the above 3-tuple is a noun that is composed of three
175`now` is the current time.
176`eny` is 512 bits of entropy for seeding random number generators.
177`beak` contains the current ship, desk, and case.
179You can access each of those pieces of data by typing their names into the Dojo.
180But in a generator, we need to put faces (variable names) onto that data so we
181can easily use it in the program. We can do so like this:
184|= [[now=@da eny=@uvJ bec=beak] ~ ~]
187Any of those pieces of data could be omitted by replacing part of the noun with
188`*` rather than giving them faces. For example, `[now=@da * bec=beak]` if we
189didn't want `eny`, or `[* * bec=beak]` if we only wanted `beak`.
191The *second part* of the sample is a `list` of arguments that _must_ be passed
192to the generator as it is run. Because it's a `list`, this element needs to be
193terminated with a `~`. In the example above, we used `~`, the empty list, to
194represent this second part. But that would mean we don't want to use any new
195faces for our generator. So a sample where we want to declare a single required
196argument would look like this:
202In the above code, we use a `*` to ignore any of the information that would go
203in the first part of the sample, somewhat similar to how we use a `~` to say
204"nothing here" for the second or third parts of the sample.
206But to use both parts together, our gate and sample would look like this:
209|= [[now=@da eny=@uvJ bec=beak] [n=@ud ~] ~]
212The *third part* of the sample is a list of _optional_ arguments. These
213arguments may be passed to the program as it's being run, but the program doesn't
214require them. The syntax for the third part is just like the syntax for the
215second part, besides its position:
221Let's look at an example that uses all three parts. Save the code below in a
222file called `dice.hoon` in your `/gen` directory.
226|= [[now=@da eny=@uvJ bec=beak] [n=@ud ~] [bet=@ud ~]]
228[(~(rad og eny) n) bet]
231This is a very simple dice program with an optional betting functionality.
232In the code, our sample specifies faces on all of the Arvo data, meaning that we
233can easily access them. We also require the argument `[n=@ud ~]`, and allow the
234_optional_ argument `[bet=@ud ~]`.
236But there's something new: `(~(rad og eny) n)`. This code pulls the `rad` arm
237out of the `og` core with the subject of `eny`. Recall that `eny` is our entropy
238value, so this is used to seed the generator. The `rad` arm will give us a
239pseudorandom number between 0 and `n`. Then we form a cell with the result and
240`bet`, the optional named argument specified previously.
242We can run this generator like so:
261We get a different value from the same generator between runs, something that
262isn't possible with a naked generator. Another novelty is the ability to choose
263to not use the second argument.
265##### Arguments without a cell
267Also unlike a naked generator, we don't need to put our arguments together into
268a cell. Swap the code below into your `add.hoon` file.
272|= [* [a=@ud b=@ud ~] ~]
277In the above code we're again creating a `%say` generator, but the sample of our
278gate is a little different than before. We are using `*` for the first part of
279the sample, because we don't use any Arvo data in this program. We are using a
280`~` for the third part, because we aren't allowing optional arguments -- the
281list of optional arguments is _empty_.
283Run a command like the one below in the Dojo. Notice that you can use two
284arguments that aren't in the cells.
293We use the `%ask` generator when we want to create an interactive program
294that prompts for inputs as it runs, rather than expecting arguments to be passed
295in at the time of initiation.
297Code-wise, two things are needed to create a useful `%ask` generator.
299The first is *`sole-result`*, which lives in `/sur/sole.hoon`. It's a
300mold-builder that's required by any part of Arvo, such as the Dojo, that wants
301to understand the output of the `%ask` generator. Because it's the only type of
302`%ask` data that is intelligible to the system, the ultimate output of an `%ask`
303generator must be `sole-result`. You can think of it as a function that takes
304molds and digests them into a type for the operating system. The simplest way to
305use `sole-result` is by calling the `produce` gate from `/lib/generators.hoon`.
307The second is *`prompt`*, a gate that lives in `lib/generators.hoon`. `prompt`
308allows you to get information back from the user, which is essential to the
309typical desired operation of an `%ask` generator.
311Since this isn't easy to digest in the abstract, we'll use an example generator
312to explain how this kind of generator works.
316The code below is an `%ask` generator that checks if the user inputs "blue" when
317prompted. Save it as `axe.hoon` in `/gen`.
325^- (sole-result (cask tang))
326%+ print leaf+"What is your favorite color?"
327%+ prompt [%& %prompt "color: "]
331 :~ leaf+"Oh. Thank you very much."
332 leaf+"Right. Off you go then."
335 leaf+"Into the Gorge of Eternal Peril with you!"
339Run the generator from the Dojo:
343What is your favorite color?
347Something new happened. Instead of simply returning something, your Dojo's
348prompt changed from `~your-urbit:dojo>` to `~your-urbit:dojo: color:`, and
349now expects additional input. Let's give it an input:
353Into the Gorge of Eternal Peril with you!
357Let's go over what exactly is happening in this code.
365Here we bring in some of the types we are going to need from `/sur/sole` and
366gates we will use from `/lib/generators`. We use some special runes for this.
368`/-` is a Ford rune used to import types from `sur`.
370`/+` is a Ford rune used to import libraries from `lib`.
372`=,` is a rune that allows us to expose a namespace. We do this to avoid having
373to write `sole-result:sole` instead of `sole-result` or `print:generators`
381This code might be familiar. Just as with their `%say` cousins, `%ask`
382generators need to produce a `cell`, the head of which specifies what kind of
383generator we are running.
385With `|= *`, we create a gate and ignore the standard arguments we are given,
386because we're not using them.
389^- (sole-result (cask tang))
392`%ask` generators need to have the second half of the cell be a gate that
393produces a `sole-result`, one that in this case contains a `cask` of `tang`.
394We use the `^-` rune to constrain the generator's output to such a
397A `cask` is a pair of a `mark` name and a noun. Recall that a `mark` can be
398thought of as an Arvo-level MIME type for data.
400A `tang` is a `list` of `tank`, and a `tank` is a structure for printing data.
401There are three types of `tank`: `leaf`, `palm`, and `rose`. A `leaf` is for
402printing a single noun, a `rose` is for printing rows of data, and a `palm` is
403for printing backstep-indented lists.
406%+ print leaf+"What is your favorite color?"
407%+ prompt [%& %prompt "color: "]
412Because we imported `generators`, we can access its contained gates, three of
413which we use in `axe.hoon`: `print`, `prompt`, and `produce`.
415*`print`* is used for printing a `tank` to the console.
417In our example, `%+` is the rune to call a gate, and our gate `print` takes one
418argument which is a `tank` to print. The `+` here is syntactic sugar for
419`[leaf "What is your favorite color?"]` that just makes it easier to write.
421*`prompt`* is used to construct a prompt for the user to provide input.
422It takes a single argument that is a tuple. Most `%ask` generators will want to
423use the `prompt` gate.
425The first element of the `prompt` sample is a flag that indicates whether what
426the user typed should be echoed out to them or hidden. `%&` will produce echoed
427output and `%|` will hide the output (for use in passwords or other secret
430The second element of the `prompt` sample is intended to be information for use
431in creating autocomplete options for the prompt. This functionality is not yet
434The third element of the `prompt` sample is the `tape` that we would like to
435use to prompt the user. In the case of our example, we use `"color: "`.
437*`produce`* is used to construct the output of the generator. In our example,
445Our gate here takes a `tape` that was produced by `prompt`. If we needed
446another type of data we could use `parse` to obtain it.
448The rest of this generator should be intelligible to those with Hoon knowledge
451One quirk that you should be aware of, though, is that `tang` prints in reverse
452order from how it is created. The reason for this is that `tang` was originally
453created to display stack trace information, which should be produced in reverse
454order. This leads to an annoyance: we either have to specify our messages
455backwards or construct them in the order we want and then `flop` the `list`.
456This is a [known issue](https://github.com/urbit/arvo/issues/840) to be resolved.
460`%get` generators are used for making HTTP requests through `eyre`. For this
461section we'll assume knowledge of generators covered in previous sections, so
462we can jump in with an example:
470^- (sole-request:sole (cask json))
471%+ curl (scan url auri:de-purl:html)
474=/ my-json (de-json:html q.u.r.hit)
484The above generator was written with the
485[Studio Ghibli API](https://ghibliapi.herokuapp.com/). It uses this API to
486print the title of a Studio Ghibli film.
494Here we import types from `/sur/sole.hoon` and gates from
495`/lib/generators.hoon`. We also use `=,` to expose the namespace of
496`generators` that we just imported.
503Like with `%say` and `%ask` generators, we must include the "boilerplate":
504code that produces a generator-specific `cell`. The head of that cell is `%get`,
505and the tail is a gate that takes a `tape` of the URL that we are trying
509^- (sole-request:sole (cask json))
512Here we make sure our gate is producing a `sole-request` which contains a `cask`
517%+ curl (scan url auri:de-purl:html)
521Here we can see the call to `curl`, the sample of which will parse our url and
522make sure it's in the proper format.
524The second part of the `curl` call is the `gate` that will get called with the
525result provided by the actual HTTP request.
531Here we verify that the `r` face of our response is not null before
532continuing. If it is null, the generator will crash, as there is nothing left to
536=/ my-json (de-json:html q.u.r.hit)
539Here we verify that data was returned to us in the `json` Hoon type.
540`q.u.r.hit` is the actual data we care about as a `cord`. `hit` is actually an
541entire response object, including headers, which we don't care about for this
542example. `de-json` out of `html` will parse the `cord` and produce a `json` type
543for us that we can then use to inspect the data.
550After parsing the JSON structure we want to make sure we got back a valid `json`
551type. To do that, we test for null and crash if that test passes. If it doesn't
552pass, we use `=,` on the `dejs:format` core so that we can more easily pull arms
561Now we come to our `produce` call. Recall that `produce` is used in all the
562non-naked generators to output the final result. Here we want to produce a
563`cask` of `json`, so we use the mark `%json` to indicate that.
565Next we have what may be an unfamiliar rune: `%.`. This rune is simply `%-` with
566the argument order reversed. `%title` is the argument passed to the next gate
567that is called. `by` is the core that is the `map` engine. A Hoon `map` is a
568key-value pair structure, sometimes referred to as a dictionary in other
569languages. `got` is the arm in `by` that will produce a gate that we can call to
570access members of a given `map`.
572> The idiomatic way to write Hoon is to have the
573 "heaviest" code paths lowest in the program, which is why `%.`
574 is preferred in this case over `%-`. Use of this style is recommended, as it
575 results in code that is much easier to read.
577At the moment, however, we do not have a `map`, but instead have a `json` type.
584Here we use `om`, which is an arm in `dejs:format` that will take a `json` and
585return a `map`. `same` is a wet gate from the standard library that takes an
586argument of any type and returns it as that type. We could use any other gate to
587modify the data if that were useful for the particular data we are going to be
588processing, but here we want the data exactly as it is.
590Since `my-json` is a `unit` of `json`, the actual data we are looking for is in
591`u.my-json`. We take this and give it to the gate produced by the call to `om`.
592This gate is going to transform our data by taking in the `json` structure and
593producing a `map`. Then we can look up keys in this map using `got` from
596`%.` performs the same function as `%-`, the rune used to call a function,
597except with the arguments reversed so that the function is last and
598the arguments of that function come first. It's used here to keep with the
599principle that heaviest, or most complex, expressions should go to the bottom.
601The key we are looking up in our new `map` we've created from `my-json` is
602`%title`, which is an element in the original source, to produce the final
603result of the generator.
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