on f3ca521 help sign in
59 lines 1.9 kB history raw
1+++
2title = "2.3.2 Molds"
3weight = 28
4template = "doc.html"
5aliases = ["/docs/learn/hoon/hoon-tutorial/molds/"]
6+++
8A mold is a function that coerces a noun to a type or crashes.
10Let's take some examples from `hoon.hoon`. `|$` is a new rune as of the writing of this document so you may see this spelled in slightly different way if you don't have the latest version of `hoon.hoon`. `|$` is the mold builder rune which takes a list of molds and produces a mold.
12```hoon
13++ pair
14 |$ [head tail]
15 [p=head q=tail]
16```
18Here is a very simple mold builder. It takes two molds and produces a mold that is a pair of those with the faces `p` and `q`. An example of using this would be `(pair @ud @ud)` which would produce a mold for a cell of `@ud` and `@ud`.
20```hoon
21++ each
22 |$ [this that]
23 $% [%| p=that]
24 [%& p=this]
25 ==
26```
28`++each` is very slightly more complicated than `pair`. `$%` is a rune that is a tagged union. The mold produced by this will match either `this` or `that`. An example of this would be `(each @ud @tas)` which will match either an `@ud` or a `@tas`.
30```hoon
31++ list
32 |$ [item]
33 $@(~ [i=item t=(list item)])
34```
35Here is a mold builder you've used previously. `$@` is a rune that will match the first thing if it's sample is an atom and the second if the sample is a cell. You should be familiar at this point that a `list` is either `~` or a pair of an item and a list of item.
38```hoon
39++ lest
40 |$ [item]
41 [i/item t/(list item)]
42```
43`lest` you may have heard of as a "non empty list." You can see that it lacks the `~` case that `list` has but it matches the second part of the `list` definition. Remember that Hoon types are defined by shape. `list` could also have been defined in terms of `lest` like this:
45```hoon
46++ list
47 |$ [item]
48 $@(~ lest)
49```
51Another possible way to write it could have been
53```hoon
54++ list
55 |$ [item]
56 $@(~ [i=item t=$])
57```
59Any of these would be equivalent.