on b93c19b help sign in
420 lines 5.9 kB history raw
1+++
2title = "1c: Molds and Mold-Builders"
3weight = 3
4template = "doc.html"
5+++
6### `++bloq`
8Blocksize
10Atom representing block size. A block of size `a` has a bitwidth of `2^a`.
12#### Source
14```
15 ++ bloq @
16```
18#### Examples
20```
21 :: ++met measures how many a-bloqs long an atom is.
23 > (met 3 256)
24 2
26 > (met 3 255)
27 1
29 > (met 3 65.535)
30 2
32 > (met 2 65.536)
33 > 3
35 > (met 0 4)
36 3
38 > (met 1 4)
39 2
41 > (met 5 9.999.999.999)
42 2
44 > (met 2 0xb5)
45 2
46```
48#### Discussion
50You can think of `bloq` as a numeral system that can represent values of
51`a^2^2`, where `a` is the block size. That's because a block of size
52`a` contains `a^2` bits, and a binary number that is `b` bits wide can represent
53`b^2` values.
55A `bloq` of 0 has a bitwidth of 1. (2^0 bits; can represent 2 values.)
57A `bloq` of 1 has a bitwidth of 2. (2^1 bits; can represent 4 values.)
59A `bloq` of 2 has a bitwidth of 4. (2^2 bits; can represent 16 values.)
61A `bloq` of 3 has a bitwidth of 8. (2^3 bits; can represent 256 values.)
63And so on.
65See the [bit arithmetic](@/docs/reference/library/2c.md) section for operations that use `bloq`s.
67---
69### `++each`
71Mold of fork between two types
73A mold generator. Produces a discriminated fork between two types, defaulting to
74`a`.
76#### Source
78 ++ each
79 |*([a=mold b=mold] $%([$| p=b] [$& p=a]))
82#### Examples
84```
85 > ? *(each cord time)
86 ?({$.y p/@t} {$.n p/@da})
87 [%.y p='']
89 > ? *(each * tape)
90 ?({$.y p/*} {$.n p/""})
91 [%.y p=0]
92```
94---
95### `++gate`
97Function
99A core with one arm, `$`--the empty name--which transforms a sample noun into a
100product noun. If used dryly as a type, the subject must have a sample type of
101`*`.
104#### Source
106```
107 ++ gate
108 $-(* *)
109```
111#### Examples
113```
114 > ? gate
115 <1.rdl {* <31.ohr 1.jmk $143>}>
116 <1.rdl {* <31.ohr 1.jmk $143>}>
118 > (`gate`|=(a=* [a 'b']) 1)
119 [1 98]
121 > (`gate`|=(a=@ [a 'b']) 1)
122 ! nest-fail
123 ! exit
124```
126#### Discussion
128A `gate` is analogous to a function in other programming languages. We created
129this new jargon because other constructs in Urbit are "functions" in the
130mathematical sense. Any `gate` normalizes to an iron `gate`.
132See also: [`++lift`](../2a), [`++cork`](../2n)
134---
135### `++list`
137List
139A mold generator. Generates a mold of a null-terminated list of a
140homogenous type.
143#### Source
145```
146 ++ list
147 |*(a=mold $@($~ (lest a)))
148```
150#### Examples
152```
153 > *(list)
154 ~
156 > `(list @)`"abc"
157 ~[97 98 99]
159 > (snag 0 "abc")
160 'a'
162 > `(list @)`[1 2 'j' ~]
163 ~[1 2 106]
165```
166#### Discussion
168See also: [`++turn`](../2b), [`++snag`](../2b)
170---
171### `++lone`
173Face on mold
175A mold generator. Puts face of `p` on the passed-in mold.
177#### Source
179```
180 ++ lone
181 |*(a=mold [p=a])
182```
184#### Examples
186```
187 > ? *cord
188 @t
189 ''
190 > ? *(lone cord)
191 p/@t
192 p=''
194 > ? *(lone @)
195 p/@
196 p=0
197 > ? *@
198 @
199 0
200```
202---
203### `++pair`
205Mold of pair of types
207A mold generator. Produces a tuple of two of the types passed in.
210#### Source
212```
213 ++ pair
214 |*([a=mold b=mold] [p=a q=b])
215```
217#### Examples
219```
220 > *(pair ? cord)
221 [p=%.y q='']
223 > *(pair tape @)
224 [p="" q=0]
226```
228---
229### `++pole`
231Faceless list
233A mold generator. A `list` without the faces `i` and `t`.
235#### Source
237```
238 ++ pole
239 |*(a=mold $@($~ [a (pole a)]))
240```
243#### Examples
245```
246 > =/(a "asdf" ?>(?=(^ a) `(lest @tD)`a))
247 [i='a' t=[i='s' t=[i='d' t=[i='f' t=~]]]]
249 > `(pole char)`"asdf"
250 ['a' ['s' ['d' ['f' ~]]]]
251```
254---
255### `++qual`
257Mold of 4-type tuple
259A mold generator. Produces a tuple of four of the types passed in.
261#### Source
263```
264 ++ qual
265 |* [a=mold b=mold c=mold d=mold]
266 [p=a q=b r=c s=d]
267```
269#### Examples
271```
272 > *(qual date time tape cord)
273 [p=[[a=%.y y=0] m=0 t=[d=0 h=0 m=0 s=0 f=~]] q=~292277024401-.1.1 r="" s='']
275 > *(qual (list @) @u @ud @ux)
276 [p=~ q=0 r=0 s=0x0]
277```
279---
280### `++quip`
282Mold of pair of `list` and type
284A mold generator. Produces a tuple of a `list` of `a` and the mold of `b`.
286#### Source
288```
289 ++ quip
290 |*([a=mold b=mold] [(list a) b])
291```
293#### Examples
295```
296 > *(quip @ _'hi')
297 [~ 'hi']
299 > `(quip @ @ux)`[[1 2 3 4 ~] 0x10]
300 [~[1 2 3 4] 0x10]
301```
303#### Discussion
305A common pattern in Hoon code is to return a `list` of changes, along with a
306new state. You'll often see `quip` used in Gall apps.
308---
309### `++trap`
311Core with one arm `$`
313A trap is a core with one arm `$`.
315#### Source
317```
318 ++ trap
319 |*(a=mold _|?(*a))
320```
322#### Examples
324```
325 > (*trap)
326 <1?dxy 1.oeg {a/<1|rbb {* <31.ohr 1.jmk $143>}> <31.ohr 1.jmk $143>}>
328 > *(*trap)
329 0
331 > (|.(42))
332 42
333```
336---
337### `++tree`
339Tree mold generator
341A mold generator. A `tree` can be empty, or contain a node of a type and
342left/right sub `tree` of the same type. Pretty-printed with `{}`.
344#### Source
346```
347 ++ tree
348 |*(a=mold $@($~ [n=a l=(tree a) r=(tree a)]))
349```
351#### Examples
353```
354 > `(tree [@ tape])`[[1 "hi"] [[2 "bye"] ~ ~] ~]
355 {[2 "bye"] [1 "hi"]}
356```
359---
360### `++trel`
362Mold of three types
364A mold generator. A mold of the tuple of the three types passed in.
366#### Source
368```
369 ++ trel
370 |*([a=mold b=mold c=mold] [p=a q=b r=c])
371```
373#### Examples
375```
376 > *(trel @ud @t @s)
377 [p=0 q='' r=--0]
379 > *(trel cord bloq tape)
380 [p='' q=0 r=""]
381```
383---
384### `++unit`
386Maybe
388A mold generator. A `unit` is either `~` or `[~ u=a]`, where `a` is the
389type that was passed in.
392#### Source
394```
395 ++ unit
396 |*(a=mold $@($~ [$~ u=a]))
397```
399#### Examples
401```
402 > ? *(unit time)
403 ?({$~ u/@da} $~)
404 ~
406 > =a |= a=@
407 ^- (unit @)
408 ?~ a ~
409 [~ a]
410 > (a 2)
411 [~ u=2]
412```
414#### Discussion
416Using a `unit` allows you to specify something that may not be there.
418See also: [`++bind`](../2b)
420---