on 69fd27d help sign in
421 lines 6.0 kB history raw
1+++
2title = "1c: Molds and Mold-Builders"
3weight = 3
4template = "doc.html"
5+++
7### `++bloq`
9Blocksize
11Atom representing block size. A block of size `a` has a bitwidth of `2^a`.
13#### Source
15```hoon
16 ++ bloq @
17```
19#### Examples
21```
22 :: ++met measures how many a-bloqs long an atom is.
24 > (met 3 256)
25 2
27 > (met 3 255)
28 1
30 > (met 3 65.535)
31 2
33 > (met 2 65.536)
34 > 3
36 > (met 0 4)
37 3
39 > (met 1 4)
40 2
42 > (met 5 9.999.999.999)
43 2
45 > (met 2 0xb5)
46 2
47```
49#### Discussion
51You can think of `bloq` as a numeral system that can represent values of
52`a^2^2`, where `a` is the block size. That's because a block of size
53`a` contains `a^2` bits, and a binary number that is `b` bits wide can represent
54`b^2` values.
56A `bloq` of 0 has a bitwidth of 1. (2^0 bits; can represent 2 values.)
58A `bloq` of 1 has a bitwidth of 2. (2^1 bits; can represent 4 values.)
60A `bloq` of 2 has a bitwidth of 4. (2^2 bits; can represent 16 values.)
62A `bloq` of 3 has a bitwidth of 8. (2^3 bits; can represent 256 values.)
64And so on.
66See the [bit arithmetic](@/docs/hoon/reference/stdlib/2c.md) section for operations that use `bloq`s.
68---
70### `++each`
72Mold of fork between two types
74A mold generator. Produces a discriminated fork between two types, defaulting to
75`a`.
77#### Source
79 ++ each
80 |*([a=mold b=mold] $%([$| p=b] [$& p=a]))
83#### Examples
85```
86 > ? *(each cord time)
87 ?({$.y p/@t} {$.n p/@da})
88 [%.y p='']
90 > ? *(each * tape)
91 ?({$.y p/*} {$.n p/""})
92 [%.y p=0]
93```
95---
96### `++gate`
98Function
100A core with one arm, `$`--the empty name--which transforms a sample noun into a
101product noun. If used dryly as a type, the subject must have a sample type of
102`*`.
105#### Source
107```hoon
108 ++ gate
109 $-(* *)
110```
112#### Examples
114```
115 > ? gate
116 <1.rdl {* <31.ohr 1.jmk $143>}>
117 <1.rdl {* <31.ohr 1.jmk $143>}>
119 > (`gate`|=(a=* [a 'b']) 1)
120 [1 98]
122 > (`gate`|=(a=@ [a 'b']) 1)
123 ! nest-fail
124 ! exit
125```
127#### Discussion
129A `gate` is analogous to a function in other programming languages. We created
130this new jargon because other constructs in Urbit are "functions" in the
131mathematical sense. Any `gate` normalizes to an iron `gate`.
133See also: [`++lift`](../2a), [`++cork`](../2n)
135---
136### `++list`
138List
140A mold generator. Generates a mold of a null-terminated list of a
141homogenous type.
144#### Source
146```hoon
147 ++ list
148 |*(a=mold $@($~ (lest a)))
149```
151#### Examples
153```
154 > *(list)
155 ~
157 > `(list @)`"abc"
158 ~[97 98 99]
160 > (snag 0 "abc")
161 'a'
163 > `(list @)`[1 2 'j' ~]
164 ~[1 2 106]
166```
167#### Discussion
169See also: [`++turn`](../2b), [`++snag`](../2b)
171---
172### `++lone`
174Face on mold
176A mold generator. Puts face of `p` on the passed-in mold.
178#### Source
180```hoon
181 ++ lone
182 |*(a=mold [p=a])
183```
185#### Examples
187```
188 > ? *cord
189 @t
190 ''
191 > ? *(lone cord)
192 p/@t
193 p=''
195 > ? *(lone @)
196 p/@
197 p=0
198 > ? *@
199 @
200 0
201```
203---
204### `++pair`
206Mold of pair of types
208A mold generator. Produces a tuple of two of the types passed in.
211#### Source
213```hoon
214 ++ pair
215 |*([a=mold b=mold] [p=a q=b])
216```
218#### Examples
220```
221 > *(pair ? cord)
222 [p=%.y q='']
224 > *(pair tape @)
225 [p="" q=0]
227```
229---
230### `++pole`
232Faceless list
234A mold generator. A `list` without the faces `i` and `t`.
236#### Source
238```hoon
239 ++ pole
240 |*(a=mold $@($~ [a (pole a)]))
241```
244#### Examples
246```
247 > =/(a "asdf" ?>(?=(^ a) `(lest @tD)`a))
248 [i='a' t=[i='s' t=[i='d' t=[i='f' t=~]]]]
250 > `(pole char)`"asdf"
251 ['a' ['s' ['d' ['f' ~]]]]
252```
255---
256### `++qual`
258Mold of 4-type tuple
260A mold generator. Produces a tuple of four of the types passed in.
262#### Source
264```hoon
265 ++ qual
266 |* [a=mold b=mold c=mold d=mold]
267 [p=a q=b r=c s=d]
268```
270#### Examples
272```
273 > *(qual date time tape cord)
274 [p=[[a=%.y y=0] m=0 t=[d=0 h=0 m=0 s=0 f=~]] q=~292277024401-.1.1 r="" s='']
276 > *(qual (list @) @u @ud @ux)
277 [p=~ q=0 r=0 s=0x0]
278```
280---
281### `++quip`
283Mold of pair of `list` and type
285A mold generator. Produces a tuple of a `list` of `a` and the mold of `b`.
287#### Source
289```hoon
290 ++ quip
291 |*([a=mold b=mold] [(list a) b])
292```
294#### Examples
296```
297 > *(quip @ _'hi')
298 [~ 'hi']
300 > `(quip @ @ux)`[[1 2 3 4 ~] 0x10]
301 [~[1 2 3 4] 0x10]
302```
304#### Discussion
306A common pattern in Hoon code is to return a `list` of changes, along with a
307new state. You'll often see `quip` used in Gall apps.
309---
310### `++trap`
312Core with one arm `$`
314A trap is a core with one arm `$`.
316#### Source
318```hoon
319 ++ trap
320 |*(a=mold _|?(*a))
321```
323#### Examples
325```
326 > (*trap)
327 <1?dxy 1.oeg {a/<1|rbb {* <31.ohr 1.jmk $143>}> <31.ohr 1.jmk $143>}>
329 > *(*trap)
330 0
332 > (|.(42))
333 42
334```
337---
338### `++tree`
340Tree mold generator
342A mold generator. A `tree` can be empty, or contain a node of a type and
343left/right sub `tree` of the same type. Pretty-printed with `{}`.
345#### Source
347```hoon
348 ++ tree
349 |*(a=mold $@($~ [n=a l=(tree a) r=(tree a)]))
350```
352#### Examples
354```
355 > `(tree [@ tape])`[[1 "hi"] [[2 "bye"] ~ ~] ~]
356 {[2 "bye"] [1 "hi"]}
357```
360---
361### `++trel`
363Mold of three types
365A mold generator. A mold of the tuple of the three types passed in.
367#### Source
369```hoon
370 ++ trel
371 |*([a=mold b=mold c=mold] [p=a q=b r=c])
372```
374#### Examples
376```
377 > *(trel @ud @t @s)
378 [p=0 q='' r=--0]
380 > *(trel cord bloq tape)
381 [p='' q=0 r=""]
382```
384---
385### `++unit`
387Maybe
389A mold generator. A `unit` is either `~` or `[~ u=a]`, where `a` is the
390type that was passed in.
393#### Source
395```hoon
396 ++ unit
397 |*(a=mold $@($~ [$~ u=a]))
398```
400#### Examples
402```
403 > ? *(unit time)
404 ?({$~ u/@da} $~)
405 ~
407 > =a |= a=@
408 ^- (unit @)
409 ?~ a ~
410 [~ a]
411 > (a 2)
412 [~ u=2]
413```
415#### Discussion
417Using a `unit` allows you to specify something that may not be there.
419See also: [`++bind`](../2b)
421---