on master help sign in
Update 1c.md

Updated syntax, fixed code, explained more. Also used `#` headers to make subheads smaller.

commit0338bb04c871391be9316b8b93e9131bc0d18a32 authorrmariani <robert@tlon.io> dateWed 23 May 2018, 16:58 (8 years ago) parent63b60ca
1 file changed+185−134
docs/hoon/library/1c.md+185−134
docs/hoon/library/1c.md+185−134
@@ -9,137 +9,201 @@ navhome: /docs/
99
1010Blocksize
1111
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`.
1313
14−Source
15−------
14+#### Source
1615
17− ++ bloq @ :: blockclass
16+ ++ bloq @
1817
19−Examples
20−--------
18+#### Examples
2119
22− > :: ++met measures how many bloqs long an atom is
23− > (met 3 256)
20+ ::++met measures how many a-bloqs long an atom is. In other words, how
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)
2439 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.
2663
2764
2865***
2966
3067### `++each`
3168
32−Mold of fork between two
69+Mold of fork between two types
3370
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`.
3573
36−Source
37−------
74+#### Source
3875
3976 ++ each
40− |* {a/$-(* *) b/$-(* *)} :: either a or b
41− $%({$& p/a} {$| p/b}) :: a default
77+ |*([a=mold b=mold] $%([$| p=b] [$& p=a]))
4278
4379
44−Examples
45−--------
80+#### Examples
4681
4782 > ? *(each cord time)
4883 ?({$.y p/@t} {$.n p/@da})
4984 [%.y p='']
5085
86+ > ? *(each * tape)
87+ ?({$.y p/*} {$.n p/""})
88+ [%.y p=0]
89+
5190***
5291### `++gate`
5392
5493Function
5594
56−A core with one arm, `$`--the empty name--which transforms a sample noun into a product
57−noun. If used dryly as a type, the subject must have a sample type of `*`.
95+A core with one arm, `$`--the empty name--which transforms a sample noun into a
96+product noun. If used dryly as a type, the subject must have a sample type of
97+`*`.
5898
5999
60−Source
61−------
100+#### Source
62101
63− ++ gate $-(* *) :: general gate
102+ ++ gate
103+ $-(* *)
64104
65105
66−Examples
67−--------
68−
69−See also: `++lift`, `++cork`
106+#### Examples
70107
71108 > ? gate
72− <1.ybc {* <110.jyx 1.ztu $151>}>
73− <1.ybc {* <110.jyx 1.ztu $151>}>
109+ <1.rdl {* <31.ohr 1.jmk $143>}>
110+ <1.rdl {* <31.ohr 1.jmk $143>}>
74111
75112 > (`gate`|=(a/* [a 'b']) 1)
76− [1 'b']
113+ [1 98]
114+
115+#### Discussion
77116
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.
78120
121+See also: [`++lift`](../2a), [`++cork`](../2n)
79122
80123***
81124### `++list`
82125
83126List
84127
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
86129homogenous type.
87130
88131
89−Source
90−------
91−
92− ++ list |* a/$-(* *) :: null-term list
93− $@($~ {i/a t/(list a)}) ::
132+#### Source
94133
134+ ++ list
135+ |*(a=mold $@($~ (lest a)))
95136
96−Examples
97−--------
98137
99−See also: `++turn`, `++snag`
138+#### Examples
100139
101140 > *(list)
102141 ~
142+
103143 > `(list @)`"abc"
104144 ~[97 98 99]
145+
105146 > (snag 0 "abc")
106147 'a'
107148
149+ > `(list @)`[1 2 'j' ~]
150+ ~[1 2 106]
151+
152+#### Discussion
108153
154+See also: [`++turn`](../2b), [`++snag`](../2b)
109155
110156***
111157### `++lone`
112158
113−`++lone` puts face of `p` on something.
159+Face on mold
114160
115−XX unused
161+A mold generator. Puts face of `p` on the passed-in mold.
116162
117−Source
118−------
163+#### Source
119164
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
121184
122185***
123186### `++pair`
124187
125188Mold of pair of types
126189
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.
128191
129192
130−Source
131−------
193+#### Source
132194
133− ++ pair |*({a/$-(* *) b/$-(* *)} {p/a q/b}) :: just a pair
195+ ++ pair
196+ |*([a=mold b=mold] [p=a q=b])
134197
135198
136199
137−Examples
138−--------
200+#### Examples
139201
140− > *(pair bean cord)
202+ > *(pair ? cord)
141203 [p=%.y q='']
142204
205+ > *(pair tape @)
206+ [p="" q=0]
143207
144208
145209***
@@ -147,105 +211,93 @@ Examples
147211
148212Faceless list
149213
150−A `++list` without the faces `i` and `t`.
214+A mold generator. A `list` without the faces `i` and `t`.
215+
216+#### Source
151217
152−Source
153−------
218+ ++ pole
219+ |*(a=mold $@($~ [a (pole a)]))
154220
155− ++ pole |* a/$-(* *) :: nameless list
156− $@($~ {a (pole a)}) ::
157221
158222
223+#### Examples
159224
160−Examples
161−--------
225+ > =/(a "asdf" ?>(?=(^ a) `(lest @tD)`a))
226+ [i='a' t=[i='s' t=[i='d' t=[i='f' t=~]]]]
162227
163228 > `(pole char)`"asdf"
164− [~~a [~~s [~~d [~~f ~]]]]
229+ ['a' ['s' ['d' ['f' ~]]]]
165230
166231
167232
168233***
169234### `++qual`
170235
171−Mold of 4 type tuple
236+Mold of 4-type tuple
172237
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.
174239
175−Source
176−------
240+#### Source
177241
178− ++ qual |* {a/$-(* *) b/$-(* *) c/$-(* *) d/$-(* *)} :: just a quadruple
179− {p/a q/b r/c s/d} ::
242+ ++ qual
243+ |* [a=mold b=mold c=mold d=mold]
244+ [p=a q=b r=c s=d]
180245
181−Examples
182−--------
246+#### Examples
183247
184248 > *(qual date time tape cord)
185249 [p=[[a=%.y y=0] m=0 t=[d=0 h=0 m=0 s=0 f=~]] q=~292277024401-.1.1 r="" s='']
186250
187−
188−
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−
251+ > *(qual (list @) @u @ud @ux)
252+ [p=~ q=0 r=0 s=0x0]
207253
208254
209255***
210256### `++quip`
211257
212−For use with `=^` `:sip`.
258+Mold of pair of `list` and type
213259
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`.
215261
216−Source
217−------
262+#### Source
218263
219− ++ quip |*({a/mold b/*} {(list a) _b}) :: list-mixed for sip
264+ ++ quip
265+ |*([a=mold b=mold] [(list a) b])
220266
221−Examples
222−--------
267+#### Examples
223268
224− > *(quip @ 'hi')
269+ > *(quip @ _'hi')
225270 [~ 'hi']
226271
272+ > `(quip @ @ux)`[[1 2 3 4 ~] 0x10]
273+ [~[1 2 3 4] 0x10]
274+
275+#### Discussion
227276
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.
228279
229− ***
280+***
230281### `++trap`
231282
232283Core with one arm `$`
233284
234−A trap is a core with one arm `++$`.
285+A trap is a core with one arm `$`.
235286
236−Source
237−------
287+#### Source
238288
239− ++ trap |*(a/_* _|?(*a)) :: makes perfect sense
289+ ++ trap
290+ |*(a=mold _|?(*a))
240291
241292
242−Examples
243−--------
293+#### Examples
244294
245− > *trap
246− <1.mws 101.jzo 1.ypj %164>
247295 > (*trap)
296+ <1?dxy 1.oeg {a/<1|rbb {* <31.ohr 1.jmk $143>}> <31.ohr 1.jmk $143>}>
297+
298+ > *(*trap)
248299 0
300+
249301 > (|.(42))
250302 42
251303
@@ -256,20 +308,18 @@ Examples
256308
257309Tree mold generator
258310
259−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 `{}`.
311+A mold generator. A `tree` can be empty, or contain a node of a type and
312+left/right sub `tree` of the same type. Pretty-printed with `{}`.
261313
262−Source
263−------
314+#### Source
264315
265− ++ tree |* a/$-(* *) :: binary tree
266− $@($~ {n/a l/(tree a) r/(tree a)}) ::
316+ ++ tree
317+ |*(a=mold $@($~ [n=a l=(tree a) r=(tree a)]))
267318
268319
269−Examples
270−--------
320+#### Examples
271321
272− > `(tree {@ tape})`[[1 "hi"] [[2 "bye"] ~ ~] ~]
322+ > `(tree [@ tape])`[[1 "hi"] [[2 "bye"] ~ ~] ~]
273323 {[2 "bye"] [1 "hi"]}
274324
275325
@@ -279,21 +329,21 @@ Examples
279329
280330Mold of three types
281331
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.
283333
284−Source
285−------
334+#### Source
286335
287− ++ trel |* {a/$-(* *) b/$-(* *) c/$-(* *)} :: just a triple
288− {p/a q/b r/c} ::
336+ ++ trel
337+ |*([a=mold b=mold c=mold] [p=a q=b r=c])
289338
290339
291−Examples
292−--------
340+#### Examples
293341
294342 > *(trel @ud @t @s)
295343 [p=0 q='' r=--0]
296344
345+ > *(trel cord bloq tape)
346+ [p='' q=0 r=""]
297347
298348
299349***
@@ -301,32 +351,33 @@ Examples
301351
302352Maybe
303353
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
305355type that was passed in.
306356
307357
308−Source
309−------
310−
311− ++ unit |* a/$-(* *) :: maybe
312− $@($~ {$~ u/a}) ::
358+#### Source
313359
360+ ++ unit
361+ |*(a=mold $@($~ [$~ u=a]))
314362
315−Examples
316−--------
363+#### Examples
317364
318−See also: `++bind`
319365
320366 > ? *(unit time)
321− ?({$~ u/@da} $~)
322− ~
367+ ?({$~ u/@da} $~)
368+ ~
323369
324− > > =a |= a/@
325− ^- (unit @)
326− ?~ a ~
327− [~ a]
370+ > =a |= a=@
371+ ^- (unit @)
372+ ?~ a ~
373+ [~ a]
328374 > (a 2)
329375 [~ u=2]
330376
377+#### Discussion
378+
379+Using a `unit` allows you to specify something that may not be there.
380+
381+See also: [`++bind`](../2b)
331382
332383***