on master help sign in
Update 1c.md
0338bb0rmariani
docs/hoon/library/1c.md+185−134
@@ -9,137 +9,201 @@ navhome: /docs/
9
9
10Blocksize
10Blocksize
11
11
12−Atom representing a blocksize, by convention expressed as a power of 2.
12+Atom representing block size. A block of size `a` has a bitwidth of `2^a`.
13
13
14−Source
14+#### Source
15−------
16
15
17− ++ bloq @ :: blockclass
16+ ++ bloq @
18
17
19−Examples
18+#### Examples
20−--------
21
19
22− > :: ++met measures how many bloqs long an atom is
20+ ::++met measures how many a-bloqs long an atom is. In other words, how
23− > (met 3 256)
21+ many bloqs of size a would be needed to represent b.
22+
23+ > (met 3 256) ::one 3-bloq has a bidwidth of 8, which can represent 0-255
24+ 2
25+
26+ > (met 3 255)
27+ 1
28+
29+ > (met 3 65.535) ::two 3-bloqs together have a bidwidth of 16, which can represent 0-65,535
30+ > 2
31+
32+ > (met 2 65.536)
33+ > 3
34+
35+ > (met 0 4)
36+ 3
37+
38+ > (met 1 4)
24 2
39 2
25− > :: 256 is 2 bloqs of 2^3
40+
41+ > (met 5 9.999.999.999)
42+ 2
43+
44+ > (met 2 0xb5)
45+ 2
46+
47+#### Discussion
48+
49+You can think of `bloq` as a numeral system that can represent values of
50+`a^2^2`, where `a` is the size of a given block. That's because a block of size
51+`a` is `a^2` bits wide, and a binary number that is `b` bits wide can represent
52+`b^2` values.
53+
54+A `bloq` of 0 has a bitwidth of 1. (2^0 bits; can represent 2 values.)
55+
56+A `bloq` of 1 has a bitwidth of 2. (2^1 bits; can represent 4 values.)
57+
58+A `bloq` of 2 has a bitwidth of 4. (2^2 bits; can represent 16 values.)
59+
60+A `bloq` of 3 has a bitwidth of 8. (2^3 bits; can represent 256 values.)
61+
62+And so on.
26
63
27
64
28***
65***
29
66
30### `++each`
67### `++each`
31
68
32−Mold of fork between two
69+Mold of fork between two types
33
70
34−mold generator: produces a discriminated fork between two types.
71+A mold generator. Produces a discriminated fork between two types, defaulting to
72+`a`.
35
73
36−Source
74+#### Source
37−------
38
75
39 ++ each
76 ++ each
40− |* {a/$-(* *) b/$-(* *)} :: either a or b
77+ |*([a=mold b=mold] $%([$| p=b] [$& p=a]))
41− $%({$& p/a} {$| p/b}) :: a default
42
78
43
79
44−Examples
80+#### Examples
45−--------
46
81
47 > ? *(each cord time)
82 > ? *(each cord time)
48 ?({$.y p/@t} {$.n p/@da})
83 ?({$.y p/@t} {$.n p/@da})
49 [%.y p='']
84 [%.y p='']
50
85
86+ > ? *(each * tape)
87+ ?({$.y p/*} {$.n p/""})
88+ [%.y p=0]
89+
51***
90***
52### `++gate`
91### `++gate`
53
92
54Function
93Function
55
94
56−A core with one arm, `$`--the empty name--which transforms a sample noun into a product
95+A core with one arm, `$`--the empty name--which transforms a sample noun into a
57−noun. If used dryly as a type, the subject must have a sample type of `*`.
96+product noun. If used dryly as a type, the subject must have a sample type of
97+`*`.
58
98
59
99
60−Source
100+#### Source
61−------
62
101
63− ++ gate $-(* *) :: general gate
102+ ++ gate
103+ $-(* *)
64
104
65
105
66−Examples
106+#### Examples
67−--------
68−
69−See also: `++lift`, `++cork`
70
107
71 > ? gate
108 > ? gate
72− <1.ybc {* <110.jyx 1.ztu $151>}>
109+ <1.rdl {* <31.ohr 1.jmk $143>}>
73− <1.ybc {* <110.jyx 1.ztu $151>}>
110+ <1.rdl {* <31.ohr 1.jmk $143>}>
74
111
75 > (`gate`|=(a/* [a 'b']) 1)
112 > (`gate`|=(a/* [a 'b']) 1)
76− [1 'b']
113+ [1 98]
114+
115+#### Discussion
77
116
117+A `gate` is analogous to a function in other programming languages. We created
118+this new jargon because other constructs in Urbit are "functions" in the
119+mathematical sense.
78
120
121+See also: [`++lift`](../2a), [`++cork`](../2n)
79
122
80***
123***
81### `++list`
124### `++list`
82
125
83List
126List
84
127
85−mold generator. `++list` generates a mold of a null-termanated list of a
128+A mold generator. Generates a mold of a null-terminated list of a
86homogenous type.
129homogenous type.
87
130
88
131
89−Source
132+#### Source
90−------
91−
92− ++ list |* a/$-(* *) :: null-term list
93− $@($~ {i/a t/(list a)}) ::
94
133
134+ ++ list
135+ |*(a=mold $@($~ (lest a)))
95
136
96−Examples
97−--------
98
137
99−See also: `++turn`, `++snag`
138+#### Examples
100
139
101 > *(list)
140 > *(list)
102 ~
141 ~
142+
103 > `(list @)`"abc"
143 > `(list @)`"abc"
104 ~[97 98 99]
144 ~[97 98 99]
145+
105 > (snag 0 "abc")
146 > (snag 0 "abc")
106 'a'
147 'a'
107
148
149+ > `(list @)`[1 2 'j' ~]
150+ ~[1 2 106]
151+
152+#### Discussion
108
153
154+See also: [`++turn`](../2b), [`++snag`](../2b)
109
155
110***
156***
111### `++lone`
157### `++lone`
112
158
113−`++lone` puts face of `p` on something.
159+Face on mold
114
160
115−XX unused
161+A mold generator. Puts face of `p` on the passed-in mold.
116
162
117−Source
163+#### Source
118−------
119
164
120− ++ lone |*(a/$-(* *) p/a) :: just one thing
165+ ++ lone
166+ |*(a=mold [p=a])
167+
168+#### Examples
169+
170+
171+ > ? *cord
172+ @t
173+ ''
174+ > ? *(lone cord)
175+ p/@t
176+ p=''
177+
178+ > ? *(lone @)
179+ p/@
180+ p=0
181+ > ? *@
182+ @
183+ 0
121
184
122***
185***
123### `++pair`
186### `++pair`
124
187
125Mold of pair of types
188Mold of pair of types
126
189
127−mold generator. Produces a tuple of two of the types passed in.
190+A mold generator. Produces a tuple of two of the types passed in.
128
191
129
192
130−Source
193+#### Source
131−------
132
194
133− ++ pair |*({a/$-(* *) b/$-(* *)} {p/a q/b}) :: just a pair
195+ ++ pair
196+ |*([a=mold b=mold] [p=a q=b])
134
197
135
198
136
199
137−Examples
200+#### Examples
138−--------
139
201
140− > *(pair bean cord)
202+ > *(pair ? cord)
141 [p=%.y q='']
203 [p=%.y q='']
142
204
205+ > *(pair tape @)
206+ [p="" q=0]
143
207
144
208
145***
209***
@@ -147,105 +211,93 @@ Examples
147
211
148Faceless list
212Faceless list
149
213
150−A `++list` without the faces `i` and `t`.
214+A mold generator. A `list` without the faces `i` and `t`.
215+
216+#### Source
151
217
152−Source
218+ ++ pole
153−------
219+ |*(a=mold $@($~ [a (pole a)]))
154
220
155− ++ pole |* a/$-(* *) :: nameless list
156− $@($~ {a (pole a)}) ::
157
221
158
222
223+#### Examples
159
224
160−Examples
225+ > =/(a "asdf" ?>(?=(^ a) `(lest @tD)`a))
161−--------
226+ [i='a' t=[i='s' t=[i='d' t=[i='f' t=~]]]]
162
227
163 > `(pole char)`"asdf"
228 > `(pole char)`"asdf"
164− [~~a [~~s [~~d [~~f ~]]]]
229+ ['a' ['s' ['d' ['f' ~]]]]
165
230
166
231
167
232
168***
233***
169### `++qual`
234### `++qual`
170
235
171−Mold of 4 type tuple
236+Mold of 4-type tuple
172
237
173−A `++qual` is a tuple of four of the types passed in.
238+A mold generator. Produces a tuple of four of the types passed in.
174
239
175−Source
240+#### Source
176−------
177
241
178− ++ qual |* {a/$-(* *) b/$-(* *) c/$-(* *) d/$-(* *)} :: just a quadruple
242+ ++ qual
243+ |* [a=mold b=mold c=mold d=mold]
179− {p/a q/b r/c s/d} ::
244+ [p=a q=b r=c s=d]
180
245
181−Examples
246+#### Examples
182−--------
183
247
184 > *(qual date time tape cord)
248 > *(qual date time tape cord)
185 [p=[[a=%.y y=0] m=0 t=[d=0 h=0 m=0 s=0 f=~]] q=~292277024401-.1.1 r="" s='']
249 [p=[[a=%.y y=0] m=0 t=[d=0 h=0 m=0 s=0 f=~]] q=~292277024401-.1.1 r="" s='']
186
250
187−
251+ > *(qual (list @) @u @ud @ux)
188−
252+ [p=~ q=0 r=0 s=0x0]
189−***
190−### `++quid`
191−
192−For use with `=^` `:sip`.
193−
194−Mold generator. Produces a tuple of `a` and the mold of `b`.
195−
196−Source
197−------
198−
199− ++ quid |*({a/mold b/*} {a _b}) :: mixed for sip
200−
201−Examples
202−--------
203−
204− > *(quid @ 'hi')
205− [0 'hi']
206−
207
253
208
254
209***
255***
210### `++quip`
256### `++quip`
211
257
212−For use with `=^` `:sip`.
258+Mold of pair of `list` and type
213
259
214−Mold generator. Produces a tuple of a list of `a` and the mold of `b`.
260+A mold generator. Produces a tuple of a list of `a` and the mold of `b`.
215
261
216−Source
262+#### Source
217−------
218
263
264+ ++ quip
219− ++ quip |*({a/mold b/*} {(list a) _b}) :: list-mixed for sip
265+ |*([a=mold b=mold] [(list a) b])
220
266
221−Examples
267+#### Examples
222−--------
223
268
224− > *(quip @ 'hi')
269+ > *(quip @ _'hi')
225 [~ 'hi']
270 [~ 'hi']
226
271
272+ > `(quip @ @ux)`[[1 2 3 4 ~] 0x10]
273+ [~[1 2 3 4] 0x10]
274+
275+#### Discussion
227
276
277+A common pattern in Hoon code is to return a `list` of changes, along with a
278+new state. You'll often see `quip` it used in Gall apps.
228
279
229− ***
280+***
230### `++trap`
281### `++trap`
231
282
232Core with one arm `$`
283Core with one arm `$`
233
284
234−A trap is a core with one arm `++$`.
285+A trap is a core with one arm `$`.
235
286
236−Source
287+#### Source
237−------
238
288
239− ++ trap |*(a/_* _|?(*a)) :: makes perfect sense
289+ ++ trap
290+ |*(a=mold _|?(*a))
240
291
241
292
242−Examples
293+#### Examples
243−--------
244
294
245− > *trap
246− <1.mws 101.jzo 1.ypj %164>
247 > (*trap)
295 > (*trap)
296+ <1?dxy 1.oeg {a/<1|rbb {* <31.ohr 1.jmk $143>}> <31.ohr 1.jmk $143>}>
297+
298+ > *(*trap)
248 0
299 0
300+
249 > (|.(42))
301 > (|.(42))
250 42
302 42
251
303
@@ -256,20 +308,18 @@ Examples
256
308
257Tree mold generator
309Tree mold generator
258
310
259−A `++tree` can be empty, or contain a node of a type and
311+A mold generator. A `tree` can be empty, or contain a node of a type and
260−left/right sub `++tree` of the same type. Pretty-printed with `{}`.
312+left/right sub `tree` of the same type. Pretty-printed with `{}`.
261
313
262−Source
314+#### Source
263−------
264
315
265− ++ tree |* a/$-(* *) :: binary tree
316+ ++ tree
266− $@($~ {n/a l/(tree a) r/(tree a)}) ::
317+ |*(a=mold $@($~ [n=a l=(tree a) r=(tree a)]))
267
318
268
319
269−Examples
320+#### Examples
270−--------
271
321
272− > `(tree {@ tape})`[[1 "hi"] [[2 "bye"] ~ ~] ~]
322+ > `(tree [@ tape])`[[1 "hi"] [[2 "bye"] ~ ~] ~]
273 {[2 "bye"] [1 "hi"]}
323 {[2 "bye"] [1 "hi"]}
274
324
275
325
@@ -279,21 +329,21 @@ Examples
279
329
280Mold of three types
330Mold of three types
281
331
282−`++mold` of the tuple of the three types passed in.
332+A mold generator. A mold of the tuple of the three types passed in.
283
333
284−Source
334+#### Source
285−------
286
335
287− ++ trel |* {a/$-(* *) b/$-(* *) c/$-(* *)} :: just a triple
336+ ++ trel
288− {p/a q/b r/c} ::
337+ |*([a=mold b=mold c=mold] [p=a q=b r=c])
289
338
290
339
291−Examples
340+#### Examples
292−--------
293
341
294 > *(trel @ud @t @s)
342 > *(trel @ud @t @s)
295 [p=0 q='' r=--0]
343 [p=0 q='' r=--0]
296
344
345+ > *(trel cord bloq tape)
346+ [p='' q=0 r=""]
297
347
298
348
299***
349***
@@ -301,32 +351,33 @@ Examples
301
351
302Maybe
352Maybe
303
353
304−mold generator. A `++unit` is either `~` or `[~ u=a]` where `a` is the
354+A mold generator. A `unit` is either `~` or `[~ u=a]`, where `a` is the
305type that was passed in.
355type that was passed in.
306
356
307
357
308−Source
358+#### Source
309−------
310−
311− ++ unit |* a/$-(* *) :: maybe
312− $@($~ {$~ u/a}) ::
313
359
360+ ++ unit
361+ |*(a=mold $@($~ [$~ u=a]))
314
362
315−Examples
363+#### Examples
316−--------
317
364
318−See also: `++bind`
319
365
320 > ? *(unit time)
366 > ? *(unit time)
321− ?({$~ u/@da} $~)
367+ ?({$~ u/@da} $~)
322− ~
368+ ~
323
369
324− > > =a |= a/@
370+ > =a |= a=@
325− ^- (unit @)
371+ ^- (unit @)
326− ?~ a ~
372+ ?~ a ~
327− [~ a]
373+ [~ a]
328 > (a 2)
374 > (a 2)
329 [~ u=2]
375 [~ u=2]
330
376
377+#### Discussion
378+
379+Using a `unit` allows you to specify something that may not be there.
380+
381+See also: [`++bind`](../2b)
331
382
332***
383***