6Generators are the most straightforward way to write programs for Urbit. They
7are used for doing computations that do not require persistence: they take an
8input and produce an output, then disappear. Generators might make sense for
9listing directory contents, or running unit tests, or fetching the contents of a
12There are four kinds of generators: naked, `%say`, `%get`, and `%ask`.
16A naked generator is simply a `gate`; that is, it is an anonymous function that
17takes a `sample` (argument) and produces a noun. All you need to do is write a
18`gate` and put it into a file in the `/gen` directory. Let's take a look at a
26This generator takes one argument of any noun and produces it without any
27changes. If we put this into a file named `echo.hoon` in the `/gen` directory,
28we can run it from the [Dojo](./docs/using/shell.md):
35This command just passes in 42 and gets 42 back. But what about when we pass in
43We just get a pile of numbers back in the form of a raw `noun`. The numbers that
44compose that noun are called `atoms`, and how they are interpreted is a matter
45of what `aura` is being applied to them. For the purposes of this lesson, an
46`aura` tells the pretty-printer how to display an `atom`, but keep in mind that
47an `aura` is type metadata and does other things, too.
49In the example above, we didn't specify an `aura`, leaving the printer to fend
50for itself. `"asdf"` is a `tape`, a type that is simply a `list` of `cords`. A
51`cord` is itself an atom represented as a string of UTF-8 characters. When used
52as part of a `tape`, the `cord` is only a single character. So each atom in the
53`[97 115 100 102 0]` output corresponds to a component of the `tape`: 97 is
54`a`, 115 is `s`, 100 is `d`, 102 is `f`, and `0` is the "null" that every list
57We can tell the Dojo to cast -- apply a specific type to -- the output of our
58generator to see something more familiar:
65Now let's create a generator with two arguments instead of one. Save the code
66below as `add.hoon` in the `/gen` directory.
73Now, run the generator:
80You may notice our generator takes a cell containing two `@ud`. This is
81actually one of the limitations of naked generators. We can only pass one
82argument. We can get around this, of course, by passing a cell, but it's less
83than ideal. Also, a naked generator cannot be called without an argument.
84This brings us to `%say` generators.
88We use `%say` generators when we want to provide something else in Arvo, the
89Urbit operating system, with metadata about the generator's output. This is
90useful when a generator is needed to pipe data to another program, a frequent
93To that end, `%say` generators use `mark`s to make it clear, to other Arvo
94computations, exactly what kind of data their output is. A `mark` is akin
95to a MIME type on the Arvo level. A `mark` describes the data in
96some way, indicating that it's an `%atom`, or that it's a standard such as
97`%json`, or even that it's an application-specific data structure like
98`%talk-command`. `mark`s are not specific to `%say` generators; whenever data
99moves between programs in Arvo, that data is marked.
101So, more formally, a `%say` generator is a `cell`. The head of that cell is the
102`%say` tag, and the tail is a `gate` that produces a `cask` -- a pair of the
103output data and the `mark` describing that data.
105Below is an example of a `%say` generator. Save it to `add.hoon` in the `/gen`
115Now run the generator as below:
122Notice that we used no argument, something that is possible with `%say`
123generators but impossible with naked generators. We'll explain that in a moment.
124For now, let's focus on the code that is necessary to make something a `%say`
131Recall that the rune `:-` produces a cell, with the first following expression
132as its head and the second following expression as its tail.
134The expression above creates a cell with `%say` as the head. The tail is
135the `|= *` expression on the line that follows.
143`|= *` constructs a gate that takes a noun. This gate will itself produce a
144`cask`, which is cell formed by the prepending `:-`. The head of that `cask` is
145`%noun` and the tail is the rest of the program, `(add 40 2)`. The tail of the
146`cask` will be our actual data produced by the body of the program: in this
147case, just adding 40 and 2 together.
149#### `%say` generators with arguments
151We can modify the boilerplate code to allow arguments to be passed into a `%say`
152generator, but in a way that gives us more power than we would have if we just
153used a naked generator.
155Naked generators are limited because they have no way of accessing data that
156exists in Arvo, such as the date and time or pieces of fresh entropy. In `%say`
157generators, however, we can access that kind of subject by identifying them
158in the gate's sample, which we only specified as `*` in the previous few
159examples. But we can do more with `%say` generators if we do more with that
160sample. Any valid sample will follow this 3-tuple scheme:
162`[[now, eny, beak] [list of unnamed arguments] [list of named arguments]]`
164This entire structure is a noun, which is why `*` is a valid sample if we
165wish to not use any of the information here in a generator. But let's look at
166each of these three elements, piece by piece.
168The **first part** of the above 3-tuple is a noun that is composed of three
171`now` is the current time.
172`eny` is 512 bits of entropy for seeding random number generators.
173`beak` contains the current ship, desk, and case.
175You can access each of those pieces of data by typing their names into the Dojo.
176But in a generator, we need to put faces (variable names) onto that data so we
177can easily use it in the program. We can do so like this:
180|= [[now=@da eny=@uvJ bec=beak] ~ ~]
183Any of those pieces of data could be omitted by replacing part of the noun with
184`*` rather than giving them faces. For example, `[now=@da * bec=beak]` if we
185didn't want `eny`, or `[* * bec=beak]` if we only wanted `beak`.
187The **second part** of the sample is a `list` of arguments that _must_ be passed
188to the generator as it is run. Because it's a `list`, this element needs to be
189terminated with a `~`. In the example above, we used `~`, the empty list, to
190represent this second part. But that would mean we don't want to use any new
191faces for our generator. So a sample where we want to declare a single required
192argument would look like this:
198In the above code, we use a `*` to ignore any of the information that would go
199in the first part of the sample, somewhat similar to how we use a `~` to say
200"nothing here" for the second or third parts of the sample.
202But to use both parts together, our gate and sample would look like this:
205|= [[now=@da eny=@uvJ bec=beak] [n=@ud ~] ~]
208The **third part** of the sample is a list of _optional_ arguments. These
209arguments may be passed to the program as it's being run, but the program doesn't
210require them. The syntax for the third part is just like the syntax for the
211second part, besides its position:
217Let's look at an example that uses all three parts. Save the code below in a
218file called `dice.hoon` in your `/gen` directory.
222|= [[now=@da eny=@uvJ bec=beak] [n=@ud ~] [bet=@ud ~]]
224[(~(rad og eny) n) bet]
227This is a very simple dice program with an optional betting functionality.
228In the code, our sample specifies faces on all of the Arvo data, meaning that we
229can easily access them. We also require the argument `[n=@ud ~]`, and allow the
230_optional_ argument `[bet=@ud ~]`.
232But there's something new: `(~(rad og eny) n)`. This code pulls the `rad` arm
233out of the `og` core with the subject of `eny`. Recall that `eny` is our entropy
234value, so this is used to seed the generator. The `rad` arm will give us a
235pseudorandom number between 0 and `n`. Then we form a cell with the result and
236`bet`, the optional named argument specified previously.
238We can run this generator like so:
257We get a different value from the same generator between runs, something that
258isn't possible with a naked generator. Another novelty is the ability to choose
259to not use the second argument.
261##### Arguments without a cell
263Also unlike a naked generator, we don't need to put our arguments together into
264a cell. Swap the code below into your `add.hoon` file.
268|= [* [a=@ud b=@ud ~] ~]
273In the above code we're again creating a `%say` generator, but the sample of our
274gate is a little different than before. We are using `*` for the first part of
275the sample, because we don't use any Arvo data in this program. We are using a
276`~` for the third part, because we aren't allowing optional arguments -- the
277list of optional arguments is _empty_.
279Run a command like the one below in the Dojo. Notice that you can use two
280arguments that aren't in the cells.
289We use the `%ask` generator when we want to create an interactive program
290that prompts for inputs as it runs, rather than expecting arguments to be passed
291in at the time of initiation.
293Code-wise, two things are needed to create a useful `%ask` generator.
295The first is **`sole-result`**, which lives in `/sur/sole.hoon`. It's a
296mold-builder that's required by any part of Arvo, such as the Dojo, that wants
297to understand the output of the `%ask` generator. Because it's the only type of
298`%ask` data that is intelligible to the system, the ultimate output of an `%ask`
299generator must be `sole-result`. You can think of it as a function that takes
300molds and digests them into a type for the operating system. The simplest way to
301use `sole-result` is by calling the `produce` gate from `/lib/generators.hoon`.
303The second is **`prompt`**, a gate that lives in `lib/generators.hoon`. `prompt`
304allows you to get information back from the user, which is essential to the
305typical desired operation of an `%ask` generator.
307Since this isn't easy to digest in the abstract, we'll use an example generator
308to explain how this kind of generator works.
312The code below is an `%ask` generator that checks if the user inputs "blue" when
313prompted. Save it as `axe.hoon` in `/gen`.
321^- (sole-result (cask tang))
322%+ print leaf+"What is your favorite color?"
323%+ prompt [%& %prompt "color: "]
327 :~ leaf+"Oh. Thank you very much."
328 leaf+"Right. Off you go then."
331 leaf+"Into the Gorge of Eternal Peril with you!"
335Run the generator from the Dojo:
339What is your favorite color?
343Something new happened. Instead of simply returning something, your Dojo's
344prompt changed from `~your-urbit:dojo>` to `~your-urbit:dojo: color:`, and
345now expects additional input. Let's give it an input:
349Into the Gorge of Eternal Peril with you!
353Let's go over what exactly is happening in this code.
361Here we bring in some of the types we are going to need from `/sur/sole` and
362gates we will use from `/lib/generators`. We use some special runes for this.
364`/-` is a Ford rune used to import types from `sur`.
366`/+` is a Ford rune used to import libraries from `lib`.
368`=,` is a rune that allows us to expose a namespace. We do this to avoid having
369to write `sole-result:sole` instead of `sole-result` or `print:generators`
377This code might be familiar. Just as with their `%say` cousins, `%ask`
378generators need to produce a `cell`, the head of which specifies what kind of
379generator we are running.
381With `|= *`, we create a gate and ignore the standard arguments we are given,
382because we're not using them.
385^- (sole-result (cask tang))
388`%ask` generators need to have the second half of the cell be a gate that
389produces a `sole-result`, one that in this case contains a `cask` of `tang`.
390We use the `^-` rune to constrain the generator's output to such a
393A `cask` is a pair of a `mark` name and a noun. Recall that a `mark` can be
394thought of as an Arvo-level MIME type for data.
396A `tang` is a `list` of `tank`, and a `tank` is a structure for printing data.
397There are three types of `tank`: `leaf`, `palm`, and `rose`. A `leaf` is for
398printing a single noun, a `rose` is for printing rows of data, and a `palm` is
399for printing backstep-indented lists.
402%+ print leaf+"What is your favorite color?"
403%+ prompt [%& %prompt "color: "]
408Because we imported `generators`, we can access its contained gates, three of
409which we use in `axe.hoon`: `print`, `prompt`, and `produce`.
411**`print`** is used for printing a `tank` to the console.
413In our example, `%+` is the rune to call a gate, and our gate `print` takes one
414argument which is a `tank` to print. The `+` here is syntactic sugar for
415`[leaf "What is your favorite color?"]` that just makes it easier to write.
417**`prompt`** is used to construct a prompt for the user to provide input.
418It takes a single argument that is a tuple. Most `%ask` generators will want to
419use the `prompt` gate.
421The first element of the `prompt` sample is a flag that indicates whether what
422the user typed should be echoed out to them or hidden. `%&` will produce echoed
423output and `%|` will hide the output (for use in passwords or other secret
426The second element of the `prompt` sample is intended to be information for use
427in creating autocomplete options for the prompt. This functionality is not yet
430The third element of the `prompt` sample is the `tape` that we would like to
431use to prompt the user. In the case of our example, we use `"color: "`.
433**`produce`** is used to construct the output of the generator. In our example,
441Our gate here takes a `tape` that was produced by `prompt`. If we needed
442another type of data we could use `parse` to obtain it.
444The rest of this generator should be intelligible to those with Hoon knowledge
447One quirk that you should be aware of, though, is that `tang` prints in reverse
448order from how it is created. The reason for this is that `tang` was originally
449created to display stack trace information, which should be produced in reverse
450order. This leads to an annoyance: we either have to specify our messages
451backwards or construct them in the order we want and then `flop` the `list`.
452This is a [known issue](https://github.com/urbit/arvo/issues/840) to be resolved.
456`%get` generators are used for making HTTP requests through `eyre`. For this
457section we'll assume knowledge of generators covered in previous sections, so
458we can jump in with an example:
466^- (sole-request:sole (cask json))
467%+ curl (scan url auri:de-purl:html)
470=/ my-json (de-json:html q.u.r.hit)
480The above generator was written with the
481[Studio Ghibli API](https://ghibliapi.herokuapp.com/). It uses this API to
482print the title of a Studio Ghibli film.
490Here we import types from `/sur/sole.hoon` and gates from
491`/lib/generators.hoon`. We also use `=,` to expose the namespace of
492`generators` that we just imported.
499Like with `%say` and `%ask` generators, we must include the "boilerplate":
500code that produces a generator-specific `cell`. The head of that cell is `%get`,
501and the tail is a gate that takes a `tape` of the URL that we are trying
505^- (sole-request:sole (cask json))
508Here we make sure our gate is producing a `sole-request` which contains a `cask`
513%+ curl (scan url auri:de-purl:html)
517Here we can see the call to `curl`, the sample of which will parse our url and
518make sure it's in the proper format.
520The second part of the `curl` call is the `gate` that will get called with the
521result provided by the actual HTTP request.
527Here we verify that the `r` face of our response is not null before
528continuing. If it is null, the generator will crash, as there is nothing left to
532=/ my-json (de-json:html q.u.r.hit)
535Here we verify that data was returned to us in the `json` Hoon type.
536`q.u.r.hit` is the actual data we care about as a `cord`. `hit` is actually an
537entire response object, including headers, which we don't care about for this
538example. `de-json` out of `html` will parse the `cord` and produce a `json` type
539for us that we can then use to inspect the data.
546After parsing the JSON structure we want to make sure we got back a valid `json`
547type. To do that, we test for null and crash if that test passes. If it doesn't
548pass, we use `=,` on the `dejs:format` core so that we can more easily pull arms
557Now we come to our `produce` call. Recall that `produce` is used in all the
558non-naked generators to output the final result. Here we want to produce a
559`cask` of `json`, so we use the mark `%json` to indicate that.
561Next we have what may be an unfamiliar rune: `%.`. This rune is simply `%-` with
562the argument order reversed. `%title` is the argument passed to the next gate
563that is called. `by` is the core that is the `map` engine. A Hoon `map` is a
564key-value pair structure, sometimes referred to as a dictionary in other
565languages. `got` is the arm in `by` that will produce a gate that we can call to
566access members of a given `map`.
568> The idiomatic way to write Hoon is to have the
569 "heaviest" code paths lowest in the program, which is why `%.`
570 is preferred in this case over `%-`. Use of this style is recommended, as it
571 results in code that is much easier to read.
573At the moment, however, we do not have a `map`, but instead have a `json` type.
580Here we use `om`, which is an arm in `dejs:format` that will take a `json` and
581return a `map`. `same` is a wet gate from the standard library that takes an
582argument of any type and returns it as that type. We could use any other gate to
583modify the data if that were useful for the particular data we are going to be
584processing, but here we want the data exactly as it is.
586Since `my-json` is a `unit` of `json`, the actual data we are looking for is in
587`u.my-json`. We take this and give it to the gate produced by the call to `om`.
588This gate is going to transform our data by taking in the `json` structure and
589producing a `map`. Then we can look up keys in this map using `got` from
592`%.` performs the same function as `%-`, the rune used to call a function,
593except with the arguments reversed so that the function is last and
594the arguments of that function come first. It's used here to keep with the
595principle that heaviest, or most complex, expressions should go to the bottom.
597The key we are looking up in our new `map` we've created from `my-json` is
598`%title`, which is an element in the original source, to produce the final
599result 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