on master help sign in
Merge pull request #641 from urbit/molds
9a771fbDavid Kerschner
learn/hoon/hoon-tutorial/molds.mdnew file+58
@@ -0,0 +1,58 @@
1++++
2+title = "Molds"
3+weight = 0
4+template = "doc.html"
5++++
6+
7+A mold is a
8+[//]: # (You should finish this thought :P)
9+
10+Let'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.
11+
12+```
13+++ pair
14+ |$ [head tail]
15+ [p=head q=tail]
16+```
17+Here 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`.
18+
19+```
20+++ each
21+ |$ [this that]
22+ $% [%| p=that]
23+ [%& p=this]
24+ ==
25+```
26+
27+`++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`.
28+
29+```
30+++ list
31+ |$ [item]
32+ $@(~ [i=item t=(list item)])
33+```
34+Here 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.
35+
36+
37+```
38+++ lest
39+ |$ [item]
40+ [i/item t/(list item)]
41+```
42+`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:
43+
44+```
45+++ list
46+ |$ [item]
47+ $@(~ lest)
48+```
49+
50+Another possible way to write it could have been
51+
52+```
53+++ list
54+ |$ [item]
55+ $@(~ [i=item t=$])
56+```
57+
58+Any of these would be equivalent.