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