on master help sign in
Merge pull request #273 from urbit/rmariani-patch-9

Update 2c.md

commite8793abd10a4ae8795e596252f704099e0dbba89 authorrmariani <robert@tlon.io> dateMon 4 Jun 2018, 18:55 (8 years ago) parentcc28838fcd63bd
1 file changed+260−188
docs/hoon/library/2c.md+260−188
docs/hoon/library/2c.md+260−188
@@ -10,38 +10,34 @@ Binary exponent
1010
1111Computes the result of `2^a`, producing an atom.
1212
13−Accepts
14−-------
13+#### Accepts
1514
1615`a` is an atom.
1716
18−Produces
19−--------
17+#### Produces
2018
2119An atom.
2220
23−Source
24−------
21+#### Source
2522
26− ++ bex :: binary exponent
23+ ++ bex
2724 ~/ %bex
28− |= a/@
25+ |= a=@
2926 ^- @
3027 ?: =(0 a) 1
3128 (mul 2 $(a (dec a)))
3229
33−Examples
34−--------
30+#### Examples
3531
3632 > (bex 4)
3733 16
34+
3835 > (bex (add 19 1))
3936 1.048.576
37+
4038 > (bex 0)
4139 1
4240
43−
44−
4541***
4642
4743### `++can`
@@ -49,44 +45,51 @@ Examples
4945Assemble
5046
5147Produces an atom from a list `b` of length-value pairs `p` and `q`,
52−where `p` is the length in bloqs of size `a`, and `q` is an atomic
48+where `p` is the length in blocks of size `a`, and `q` is an atomic
5349value.
5450
55−Accepts
56−-------
51+#### Accepts
5752
58−`a` is a block size (see `++bloq`).
53+`a` is a block size (see [`++bloq`](../1c)).
5954
60−`b` is a `++list` of length value pairs, `p` and `q`.
55+`b` is a `list` of length-value pairs, `p` and `q`.
6156
62−Produces
63−--------
57+#### Produces
6458
6559An atom.
6660
67−Source
68−------
61+#### Source
6962
70− ++ can :: assemble
63+ ++ can
7164 ~/ %can
72− |= [a/bloq b/(list [p=@ q=@])]
65+ |= [a=bloq b=(list [p=@ q=@])]
7366 ^- @
7467 ?~ b 0
75− (mix (end a p.i.b q.i.b) (lsh a p.i.b $(b t.b)))
68+ (add (end a p.i.b q.i.b) (lsh a p.i.b $(b t.b)))
7669
77−Examples
78−--------
70+#### Examples
71+
72+ > `@ub`21 :: @ub is the binary aura
73+ 0b1.0101
74+
75+ > `@ub`(can 3 ~[[1 21]])
76+ 0b1.0101
77+
78+ > `@ub`(can 3 ~[[1 1]])
79+ 0b1
7980
80− > `@ub`(can 3 ~[[1 1]])
81− 0b1
8281 > `@ub`(can 0 ~[[1 255]])
8382 0b1
84− > `@ux`(can 3 [3 0xc1] [1 0xa] ~)
83+
84+ > `@ux`(can 3 [3 0xc1] [1 0xa] ~) :: @ux is the hexadecimal aura
8585 0xa00.00c1
86+
8687 > `@ux`(can 3 [3 0xc1] [1 0xa] [1 0x23] ~)
8788 0x23.0a00.00c1
89+
8890 > `@ux`(can 4 [3 0xc1] [1 0xa] [1 0x23] ~)
8991 0x23.000a.0000.0000.00c1
92+
9093 > `@ux`(can 3 ~[[1 'a'] [2 'bc']])
9194 0x63.6261
9295
@@ -96,54 +99,60 @@ Examples
9699
97100Concatenate
98101
99−Concatenates two atoms, `b` and `c`, according to bloq size `a`, producing an
102+Concatenates two atoms, `b` and `c`, according to block size `a`, producing an
100103atom.
101104
102−Accepts
103−-------
105+#### Accepts
104106
105−`a` is a block size (see `++bloq`).
107+`a` is a block size (see [`++bloq`](../1c)).
106108
107109`b` is an atom.
108110
109111`c` is an atom.
110112
111−Produces
112−--------
113+#### Produces
113114
114115An atom.
115116
116−Source
117−------
117+#### Source
118118
119− ++ cat :: concatenate
119+ ++ cat
120120 ~/ %cat
121− |= {a/bloq b/@ c/@}
121+ |= [a=bloq b=@ c=@]
122122 (add (lsh a (met a b) c) b)
123123
124124
125−Examples
126−--------
125+#### Examples
127126
128− > `@ub`(cat 3 1 0)
127+ > `@ub`(cat 3 1 0) :: @ub is the binary aura
129128 0b1
129+
130130 > `@ub`(cat 0 1 1)
131131 0b11
132+
132133 > `@ub`(cat 0 2 1)
133134 0b110
135+
134136 > `@ub`(cat 2 1 1)
135137 0b1.0001
138+
136139 > `@ub`256
137140 0b1.0000.0000
141+
138142 > `@ub`255
139143 0b1111.1111
144+
140145 > `@ub`(cat 3 256 255)
141146 0b1111.1111.0000.0001.0000.0000
147+
142148 > `@ub`(cat 2 256 255)
143149 0b1111.1111.0001.0000.0000
150+
144151 > (cat 3 256 255)
145152 16.711.936
146153
154+ > (cat 2 256 255)
155+ 1.044.736
147156
148157
149158***
@@ -151,47 +160,53 @@ Examples
151160
152161Slice
153162
154−Slices `c` blocks of size `a` that are `b` blocks from the end of `d`.
163+Slices `c` blocks of size `a` that are positioned `b` blocks from the end of
164+`d`. That slice is produced as an atom.
155165
156−Accepts
157−-------
166+#### Accepts
158167
159−`a` is a block size (see ++bloq).
168+`a` is a block size (see [`++bloq`](../1c)).
160169
161170`b` is an atom.
162171
163172`c` is an atom.
164173
165−Produces
166−--------
174+`d` is an atom.
175+
176+#### Produces
167177
168178An atom.
169179
170−Source
171−------
180+#### Source
172181
173− ++ cut :: slice
182+ ++ cut
174183 ~/ %cut
175− |= {a/bloq {b/@u c/@u} d/@}
184+ |= [a=bloq [b=@u c=@u] d=@]
176185 (end a c (rsh a b d))
177186
178−Examples
179−--------
187+#### Examples
180188
181189 > (cut 0 [1 1] 2)
182190 1
191+
183192 > (cut 0 [2 1] 4)
184193 1
194+
185195 > `@t`(cut 3 [0 3] 'abcdefgh')
186196 'abc'
187− > `@t`(cut 3 [1 3] 'abcdefgh')
197+
198+ > `@t`(cut 3 [1 3] 'abcdefgh') :: @t is the cord aura
188199 'bcd'
189− > `@ub`(cut 0 [0 3] 0b1111.0000.1101)
200+
201+ > `@ub`(cut 0 [0 3] 0b1111.0000.1101) :: @ub is the binary aura
190202 0b101
203+
191204 > `@ub`(cut 0 [0 6] 0b1111.0000.1101)
192205 0b1101
206+
193207 > `@ub`(cut 0 [4 6] 0b1111.0000.1101)
194208 0b11.0000
209+
195210 > `@ub`(cut 0 [3 6] 0b1111.0000.1101)
196211 0b10.0001
197212
@@ -204,48 +219,60 @@ Tail
204219
205220Produces an atom by taking the last `b` blocks of size `a` from `c`.
206221
207−Accepts
208−-------
222+#### Accepts
209223
210−`a` is a block size (see `++bloq`).
224+`a` is a block size (see [`++bloq`](../1c)).
211225
212226`b` is an atom.
213227
214228`c` is an atom.
215229
216−Produces
217−--------
230+#### Produces
218231
219232An atom.
220233
221234
222−Source
223−------
235+#### Source
224236
225− ++ end :: tail
237+ ++ end
226238 ~/ %end
227− |= {a/bloq b/@u c/@}
239+ |= [a=bloq b=@u c=@]
228240 (mod c (bex (mul (bex a) b)))
229241
230242
231−Examples
232−--------
243+#### Examples
244+
245+ > (end 2 2 255)
246+ 255
247+
248+ > (end 3 1 255)
249+ 255
250+
251+ > (end 3 1 256)
252+ 0
233253
234− > `@ub`12
254+ > `@ub`12 :: @ub is the binary aura
235255 0b1100
256+
236257 > `@ub`(end 0 3 12)
237258 0b100
259+
238260 > (end 0 3 12)
239261 4
262+
240263 > `@ub`(end 1 3 12)
241264 0b1100
265+
242266 > (end 1 3 12)
243267 12
244− > `@ux`'abc'
268+
269+ > `@ux`'abc' :: @ux is the hexademical aura
245270 0x63.6261
271+
246272 > `@ux`(end 3 2 'abc')
247273 0x6261
248− > `@t`(end 3 2 'abc')
274+
275+ > `@t`(end 3 2 'abc') :: @t is the cord aura
249276 'ab'
250277
251278
@@ -257,25 +284,22 @@ Fill bloqstream
257284
258285Produces an atom by repeating `c` for `b` blocks of size `a`.
259286
260−Accepts
261−-------
287+#### Accepts
262288
263−`a` is a block size (see `++bloq`).
289+`a` is a block size (see [`++bloq`](../1c)).
264290
265291`b` is an atom.
266292
267293`c` is an atom.
268294
269−Produces
270−--------
295+#### Produces
271296
272297An atom.
273298
274−Source
275−------
299+#### Source
276300
277− ++ fil :: fill bloqstream
278− |= {a/bloq b/@u c/@}
301+ ++ fil
302+ |= [a=bloq b=@u c=@]
279303 =+ n=0
280304 =+ d=c
281305 |- ^- @
@@ -284,18 +308,23 @@ Source
284308 $(d (add c (lsh a 1 d)), n +(n))
285309
286310
287−Examples
288−--------
311+#### Examples
289312
290− > `@t`(fil 3 5 %a)
313+ > `@t`(fil 3 5 %a) :: @t is the cord (string) aura
291314 'aaaaa'
315+
292316 > `@t`(fil 5 10 %ceeb)
293317 'ceebceebceebceebceebceebceebceebceebceeb'
318+
294319 > `@t`(fil 4 10 %eced)
295320 'ʇʇʇʇʇʇʇʇʇʇed'
296− > `@tas`(fil 4 10 %bf)
321+
322+ > `@tas`(fil 4 10 %bf) :: @tas is the term aura
297323 %bfbfbfbfbfbfbfbfbfbf
298324
325+ > `@ub`(fil 2 6 1) :: @ub is the binary aura
326+ 0b1.0001.0001.0001.0001.0001
327+
299328
300329
301330***
@@ -305,42 +334,43 @@ Left-shift
305334
306335Produces an atom by left-shifting `c` by `b` blocks of size `a`.
307336
308−Accepts
309−-------
337+#### Accepts
310338
311−`a` is a block size (see `++bloq`).
339+`a` is a block size (see [`++bloq`](../1c)).
312340
313341`b` is an atom.
314342
315343`c` is an atom.
316344
317−Produces
318−--------
345+#### Produces
319346
320347An atom.
321348
322−Source
323−------
349+#### Source
324350
325− ++ lsh :: left-shift
351+ ++ lsh
326352 ~/ %lsh
327− |= {a/bloq b/@u c/@}
353+ |= [a=bloq b=@u c=@]
328354 (mul (bex (mul (bex a) b)) c)
329355
330356
331−Examples
332−--------
357+#### Examples
333358
334− > `@ub`1
359+ > `@ub`1 :: @ub is the binary aura
335360 0b1
361+
336362 > `@ub`(lsh 0 1 1)
337363 0b10
364+
338365 > (lsh 0 1 1)
339366 2
367+
340368 > `@ub`255
341369 0b1111.1111
370+
342371 > `@ub`(lsh 3 1 255)
343372 0b1111.1111.0000.0000
373+
344374 > (lsh 3 1 255)
345375 65.280
346376
@@ -353,33 +383,37 @@ Measure
353383
354384Computes the number of blocks of size `a` in `b`, producing an atom.
355385
356−`a` is a block size (see `++bloq`).
386+#### Accepts
387+
388+`a` is a block size (see [`++bloq`](../1c)).
357389
358390`b` is an atom.
359391
360−Source
361−------
392+#### Source
362393
363− ++ met :: measure
394+ ++ met
364395 ~/ %met
365− |= {a/bloq b/@}
396+ |= [a=bloq b=@]
366397 ^- @
367398 =+ c=0
368399 |-
369400 ?: =(0 b) c
370401 $(b (rsh a 1 b), c +(c))
371402
372−Examples
373−--------
403+#### Examples
374404
375405 > (met 0 1)
376406 1
407+
377408 > (met 0 2)
378409 2
410+
379411 > (met 3 255)
380412 1
413+
381414 > (met 3 256)
382415 2
416+
383417 > (met 3 'abcde')
384418 5
385419
@@ -390,43 +424,57 @@ Examples
390424
391425Assemble non-zero
392426
393−Concatenates a list of atoms `b` using blocksize `a`, producing an atom.
427+Concatenates a list of atoms `b` using block size `a`, producing an atom.
394428
395−Accepts
396−-------
429+#### Accepts
397430
398−`a` is a block size (see `++bloq`).
431+`a` is a block size (see [`++bloq`](../1c)).
399432
400−`b` is a `++list` of atoms.
433+`b` is a `list` of atoms.
401434
402−Produces
403−--------
435+#### Produces
404436
405437An atom.
406438
407−Source
408−------
439+#### Source
409440
410− ++ rap :: assemble nonzero
441+ ++ rap
411442 ~/ %rap
412− |= {a/bloq b/(list @)}
443+ |= [a=bloq b=(list @)]
413444 ^- @
445+ =+ ~
414446 ?~ b 0
415447 (cat a i.b $(b t.b))
416448
417−Examples
418−--------
449+#### Examples
419450
420− > `@ub`(rap 2 (limo [1 2 3 4 ~]))
451+ > `@ub`(rap 2 [1 2 3 4 ~]) :: @ub is the binary aura
421452 0b100.0011.0010.0001
422− > `@ub`(rap 1 (limo [1 2 3 4 ~]))
453+
454+ > `@ub`(rap 1 [1 2 3 4 ~])
423455 0b1.0011.1001
424− > (rap 0 (limo [0 0 0 ~]))
456+
457+ > (rap 0 [0 0 0 ~])
425458 0
426− > (rap 0 (limo [1 0 1 ~]))
459+
460+ > (rap 0 [1 0 1 ~])
427461 3
428462
463+ > `@ub`3
464+ 0b11
465+
466+ > (rap 0 [0 1 0 0 1 2 ~])
467+ 11
468+
469+ > (rap 0 [1 1 2 ~])
470+ 11
471+
472+ > `@ub`11
473+ 0b1011
474+
475+#### Discussion
429476
477+Any element of the value `0` is not included in concatenation.
430478
431479***
432480### `++rep`
@@ -435,48 +483,59 @@ Assemble single
435483
436484Produces an atom by assembling a list of atoms `b` using block size `a`.
437485
438−Accepts
439−-------
486+#### Accepts
440487
441−`a` is a block size (see `++bloq`).
488+`a` is a block size (see [`++bloq`](../1c)).
442489
443490`b` is a list of atoms.
444491
445−Produces
446−--------
492+#### Produces
447493
448494An atom.
449495
450−Source
451−------
496+#### Source
452497
453− ++ rep :: assemble single
498+ ++ rep
454499 ~/ %rep
455− |= {a/bloq b/(list @)}
500+ |= [a=bloq b=(list @)]
456501 ^- @
457502 =+ c=0
458503 |-
459504 ?~ b 0
460− (con (lsh a c (end a 1 i.b)) $(c +(c), b t.b))
505+ (add (lsh a c (end a 1 i.b)) $(c +(c), b t.b))
461506
462−Examples
463−--------
507+#### Examples
464508
465− > `@ub`(rep 2 (limo [1 2 3 4 ~]))
509+ > `@ub`(rep 2 [1 2 3 4 ~]) :: @ub is the binary aura
466510 0b100.0011.0010.0001
467− > (rep 0 (limo [0 0 1 ~]))
511+
512+ > (rep 0 [0 0 1 ~])
468513 4
469− > (rep 0 (limo [0 0 0 1 ~]))
514+
515+ > (rep 0 [0 0 0 1 ~])
470516 8
471− > (rep 0 (limo [0 1 0 0 ~]))
517+
518+ > `@ub`(rep 0 [0 0 0 1 ~])
519+ 0b1000
520+
521+ > `@ub`8
522+ 0b1000
523+
524+ > (rep 0 [0 1 0 0 ~])
472525 2
473− > (rep 0 (limo [0 1 0 1 ~]))
526+
527+ > (rep 0 [0 1 0 1 ~])
474528 10
475− > (rep 0 (limo [0 1 0 1 0 1 ~]))
476− 42
477− > `@ub`42
529+
530+ > (rep 0 [1 0 1 0 1 ~])
531+ 21
532+
533+ > `@ub`21
478534 0b10.1010
479535
536+ > `@ub`(rep 3 [12 166 8 34 ~])
537+ 0b10.0010.0000.1000.1010.0110.0000.1100
538+
480539
481540
482541***
@@ -486,45 +545,51 @@ Disassemble
486545
487546Produces a list of atoms from the bits of `b` using block size `a`.
488547
489−Accepts
490−-------
548+#### Accepts
491549
492−`a` is a block size (see `++bloq`).
550+`a` is a block size (see [`++bloq`](../1c)).
493551
494552`b` is an atom.
495553
496−Produces
497−--------
554+#### Produces
498555
499556A list of atoms.
500557
501−Source
502−------
558+#### Source
503559
504− ++ rip :: disassemble
560+ ++ rip
505561 ~/ %rip
506− |= {a/bloq b/@}
562+ |= [a=bloq b=@]
507563 ^- (list @)
508564 ?: =(0 b) ~
509565 [(end a 1 b) $(b (rsh a 1 b))]
510566
511−Examples
512−--------
567+#### Examples
513568
514− > `@ub`155
569+ > `@ub`155 :: @ub is the binary aura
515570 0b1001.1011
571+
516572 > (rip 0 155)
517573 ~[1 1 0 1 1 0 0 1]
574+
518575 > (rip 2 155)
519576 ~[11 9]
577+
578+ > (rip 0 11)
579+ ~[1 1 0 1]
580+
520581 > (rip 1 155)
521582 ~[3 2 1 2]
583+
522584 > `@ub`256
523585 0b1.0000.0000
586+
524587 > (rip 0 256)
525588 ~[0 0 0 0 0 0 0 0 1]
589+
526590 > (rip 2 256)
527591 ~[0 0 1]
592+
528593 > (rip 3 256)
529594 ~[0 1]
530595
@@ -537,51 +602,57 @@ Right-shift
537602
538603Right-shifts `c` by `b` blocks of size `a`, producing an atom.
539604
540−Accepts
541−-------
605+#### Accepts
542606
543−`a` is a block size (see `++bloq`).
607+`a` is a block size (see [`++bloq`](../1c)).
544608
545609`b` is an atom.
546610
547611`c` is an atom.
548612
549−Produces
550−--------
613+#### Produces
551614
552615An atom.
553616
554−Source
555−------
617+#### Source
556618
557− ++ rsh :: right-shift
619+ ++ rsh
558620 ~/ %rsh
559− |= {a/bloq b/@u c/@}
621+ |= [a=bloq b=@u c=@]
560622 (div c (bex (mul (bex a) b)))
561623
562−Examples
563−--------
624+#### Examples
564625
565− > `@ub`145
626+ > `@ub`145 :: @ub is the binary aura
566627 0b1001.0001
628+
567629 > `@ub`(rsh 1 1 145)
568630 0b10.0100
631+
569632 > (rsh 1 1 145)
570633 36
634+
571635 > `@ub`(rsh 2 1 145)
572636 0b1001
637+
573638 > (rsh 2 1 145)
574639 9
640+
575641 > `@ub`10
576642 0b1010
643+
577644 > `@ub`(rsh 0 1 10)
578645 0b101
646+
579647 > (rsh 0 1 10)
580648 5
649+
581650 > `@ux`'abc'
582651 0x63.6261
652+
583653 > `@t`(rsh 3 1 'abc')
584654 'bc'
655+
585656 > `@ux`(rsh 3 1 'abc')
586657 0x6362
587658
@@ -592,39 +663,39 @@ Examples
592663
593664Reverse block order
594665
595−Switches little endian to big and vice versa: produces an atom by
666+Switches little-endian to big-endian and vice versa: produces an atom by
596667reversing the block order of `b` using block size `a`.
597668
598669
599−Accepts
600−-------
670+#### Accepts
601671
602−`a` is a block size (see `++bloq`).
672+`a` is a block size (see [`++bloq`](../1c)).
603673
604674`b` is an atom.
605675
606−Produces
607−--------
676+#### Produces
608677
609678An atom
610679
611−Source
612−------
680+#### Source
613681
614− ++ swap |=({a/bloq b/@} (rep a (flop (rip a b)))) :: reverse bloq order
682+ ++ swp |=([a=bloq b=@] (rep a (flop (rip a b))))
615683
616−Examples
617−--------
684+#### Examples
618685
619− > `@ub`24
686+ > `@ub`24 :: @ub is the binary aura
620687 0b1.1000
621− > (swap 0 24)
688+
689+ > (swp 0 24)
622690 3
691+
623692 > `@ub`3
624693 0b11
625− > (swap 0 0)
694+
695+ > (swp 0 0)
626696 0
627− > (swap 0 128)
697+
698+ > (swp 0 128)
628699 1
629700
630701
@@ -636,38 +707,39 @@ Binary logarithm
636707
637708Computes the base-2 logarithm of `a`, producing an atom.
638709
639−Accepts
640−-------
710+#### Accepts
641711
642712`a` is an atom.
643713
644−Produces
645−--------
714+#### Produces
646715
647716An atom.
648717
649−Source
650−------
718+#### Source
651719
652− ++ xeb :: binary logarithm
720+ ++ xeb
653721 ~/ %xeb
654− |= a/@
722+ |= a=@
655723 ^- @
656724 (met 0 a)
657725
658−Examples
659−--------
726+#### Examples
660727
661728 > (xeb 31)
662729 5
730+
663731 > (xeb 32)
664732 6
733+
665734 > (xeb 49)
666735 6
736+
667737 > (xeb 0)
668738 0
739+
669740 > (xeb 1)
670741 1
742+
671743 > (xeb 2)
672744 2
673745