on c19bc7a help sign in
425 lines 6.0 kB history raw
1:- :~ navhome/'/docs/'
2 sort/'3'
3 title/'1c: Molds and Mold-Builders'
4 ==
6;>
9# 1c: molds and mold builders
11### `++bloq`
13Blocksize
15Atom representing block size. A block of size `a` has a bitwidth of `2^a`.
17#### Source
19```
20 ++ bloq @
21```
23#### Examples
25```
26 :: ++met measures how many a-bloqs long an atom is.
28 > (met 3 256)
29 2
31 > (met 3 255)
32 1
34 > (met 3 65.535)
35 2
37 > (met 2 65.536)
38 > 3
40 > (met 0 4)
41 3
43 > (met 1 4)
44 2
46 > (met 5 9.999.999.999)
47 2
49 > (met 2 0xb5)
50 2
51```
53#### Discussion
55You can think of `bloq` as a numeral system that can represent values of
56`a^2^2`, where `a` is the block size. That's because a block of size
57`a` contains `a^2` bits, and a binary number that is `b` bits wide can represent
58`b^2` values.
60A `bloq` of 0 has a bitwidth of 1. (2^0 bits; can represent 2 values.)
62A `bloq` of 1 has a bitwidth of 2. (2^1 bits; can represent 4 values.)
64A `bloq` of 2 has a bitwidth of 4. (2^2 bits; can represent 16 values.)
66A `bloq` of 3 has a bitwidth of 8. (2^3 bits; can represent 256 values.)
68And so on.
70See the [bit arithmetic](../2c) section for operations that use `bloq`s.
72---
74### `++each`
76Mold of fork between two types
78A mold generator. Produces a discriminated fork between two types, defaulting to
79`a`.
81#### Source
83 ++ each
84 |*([a=mold b=mold] $%([$| p=b] [$& p=a]))
87#### Examples
89```
90 > ? *(each cord time)
91 ?({$.y p/@t} {$.n p/@da})
92 [%.y p='']
94 > ? *(each * tape)
95 ?({$.y p/*} {$.n p/""})
96 [%.y p=0]
97```
99---
100### `++gate`
102Function
104A core with one arm, `$`--the empty name--which transforms a sample noun into a
105product noun. If used dryly as a type, the subject must have a sample type of
106`*`.
109#### Source
111```
112 ++ gate
113 $-(* *)
114```
116#### Examples
118```
119 > ? gate
120 <1.rdl {* <31.ohr 1.jmk $143>}>
121 <1.rdl {* <31.ohr 1.jmk $143>}>
123 > (`gate`|=(a=* [a 'b']) 1)
124 [1 98]
126 > (`gate`|=(a=@ [a 'b']) 1)
127 ! nest-fail
128 ! exit
129```
131#### Discussion
133A `gate` is analogous to a function in other programming languages. We created
134this new jargon because other constructs in Urbit are "functions" in the
135mathematical sense. Any `gate` normalizes to an iron `gate`.
137See also: [`++lift`](../2a), [`++cork`](../2n)
139---
140### `++list`
142List
144A mold generator. Generates a mold of a null-terminated list of a
145homogenous type.
148#### Source
150```
151 ++ list
152 |*(a=mold $@($~ (lest a)))
153```
155#### Examples
157```
158 > *(list)
159 ~
161 > `(list @)`"abc"
162 ~[97 98 99]
164 > (snag 0 "abc")
165 'a'
167 > `(list @)`[1 2 'j' ~]
168 ~[1 2 106]
170```
171#### Discussion
173See also: [`++turn`](../2b), [`++snag`](../2b)
175---
176### `++lone`
178Face on mold
180A mold generator. Puts face of `p` on the passed-in mold.
182#### Source
184```
185 ++ lone
186 |*(a=mold [p=a])
187```
189#### Examples
191```
192 > ? *cord
193 @t
194 ''
195 > ? *(lone cord)
196 p/@t
197 p=''
199 > ? *(lone @)
200 p/@
201 p=0
202 > ? *@
203 @
204 0
205```
207---
208### `++pair`
210Mold of pair of types
212A mold generator. Produces a tuple of two of the types passed in.
215#### Source
217```
218 ++ pair
219 |*([a=mold b=mold] [p=a q=b])
220```
222#### Examples
224```
225 > *(pair ? cord)
226 [p=%.y q='']
228 > *(pair tape @)
229 [p="" q=0]
231```
233---
234### `++pole`
236Faceless list
238A mold generator. A `list` without the faces `i` and `t`.
240#### Source
242```
243 ++ pole
244 |*(a=mold $@($~ [a (pole a)]))
245```
248#### Examples
250```
251 > =/(a "asdf" ?>(?=(^ a) `(lest @tD)`a))
252 [i='a' t=[i='s' t=[i='d' t=[i='f' t=~]]]]
254 > `(pole char)`"asdf"
255 ['a' ['s' ['d' ['f' ~]]]]
256```
259---
260### `++qual`
262Mold of 4-type tuple
264A mold generator. Produces a tuple of four of the types passed in.
266#### Source
268```
269 ++ qual
270 |* [a=mold b=mold c=mold d=mold]
271 [p=a q=b r=c s=d]
272```
274#### Examples
276```
277 > *(qual date time tape cord)
278 [p=[[a=%.y y=0] m=0 t=[d=0 h=0 m=0 s=0 f=~]] q=~292277024401-.1.1 r="" s='']
280 > *(qual (list @) @u @ud @ux)
281 [p=~ q=0 r=0 s=0x0]
282```
284---
285### `++quip`
287Mold of pair of `list` and type
289A mold generator. Produces a tuple of a `list` of `a` and the mold of `b`.
291#### Source
293```
294 ++ quip
295 |*([a=mold b=mold] [(list a) b])
296```
298#### Examples
300```
301 > *(quip @ _'hi')
302 [~ 'hi']
304 > `(quip @ @ux)`[[1 2 3 4 ~] 0x10]
305 [~[1 2 3 4] 0x10]
306```
308#### Discussion
310A common pattern in Hoon code is to return a `list` of changes, along with a
311new state. You'll often see `quip` used in Gall apps.
313---
314### `++trap`
316Core with one arm `$`
318A trap is a core with one arm `$`.
320#### Source
322```
323 ++ trap
324 |*(a=mold _|?(*a))
325```
327#### Examples
329```
330 > (*trap)
331 <1?dxy 1.oeg {a/<1|rbb {* <31.ohr 1.jmk $143>}> <31.ohr 1.jmk $143>}>
333 > *(*trap)
334 0
336 > (|.(42))
337 42
338```
341---
342### `++tree`
344Tree mold generator
346A mold generator. A `tree` can be empty, or contain a node of a type and
347left/right sub `tree` of the same type. Pretty-printed with `{}`.
349#### Source
351```
352 ++ tree
353 |*(a=mold $@($~ [n=a l=(tree a) r=(tree a)]))
354```
356#### Examples
358```
359 > `(tree [@ tape])`[[1 "hi"] [[2 "bye"] ~ ~] ~]
360 {[2 "bye"] [1 "hi"]}
361```
364---
365### `++trel`
367Mold of three types
369A mold generator. A mold of the tuple of the three types passed in.
371#### Source
373```
374 ++ trel
375 |*([a=mold b=mold c=mold] [p=a q=b r=c])
376```
378#### Examples
380```
381 > *(trel @ud @t @s)
382 [p=0 q='' r=--0]
384 > *(trel cord bloq tape)
385 [p='' q=0 r=""]
386```
388---
389### `++unit`
391Maybe
393A mold generator. A `unit` is either `~` or `[~ u=a]`, where `a` is the
394type that was passed in.
397#### Source
399```
400 ++ unit
401 |*(a=mold $@($~ [$~ u=a]))
402```
404#### Examples
406```
407 > ? *(unit time)
408 ?({$~ u/@da} $~)
409 ~
411 > =a |= a=@
412 ^- (unit @)
413 ?~ a ~
414 [~ a]
415 > (a 2)
416 [~ u=2]
417```
419#### Discussion
421Using a `unit` allows you to specify something that may not be there.
423See also: [`++bind`](../2b)
425---