on master help sign in
Merge pull request #283 from urbit/rmariani-patch-15
cbda13bJoshua Reagan
docs/hoon/library/2p.md+57−57
@@ -8,29 +8,25 @@ navhome: /docs/
8
8
9Unpack atom to noun
9Unpack atom to noun
10
10
11−Produces a noun unpacked from atom `a`. The inverse of jam.
11+Produces a noun unpacked from atom `a`. The inverse of `jam`.
12
12
13−Accepts
13+#### Accepts
14−-------
15
14
16−`a` is an atom.
17
15
18−Produces
16+#### Produces
19−--------
20
17
21−A `++noun`.
18+A noun.
22
19
23−Source
20+#### Source
24−------
25
21
26− ++ cue :: unpack
22+ ++ cue
27 ~/ %cue
23 ~/ %cue
28− |= a/@
24+ |= a=@
29 ^- *
25 ^- *
30 =+ b=0
26 =+ b=0
31 =+ m=`(map @ *)`~
27 =+ m=`(map @ *)`~
32 =< q
28 =< q
33− |- ^- {p/@ q/* r/(map @ *)}
29+ |- ^- [p=@ q=* r=(map @ *)]
34 ?: =(0 (cut 0 [b 1] a))
30 ?: =(0 (cut 0 [b 1] a))
35 =+ c=(rub +(b) a)
31 =+ c=(rub +(b) a)
36 [+(p.c) q.c (~(put by m) b q.c)]
32 [+(p.c) q.c (~(put by m) b q.c)]
@@ -42,18 +38,18 @@ Source
42 [(add 2 (add p.u p.v)) w (~(put by r.v) b w)]
38 [(add 2 (add p.u p.v)) w (~(put by r.v) b w)]
43 =+ d=(rub c a)
39 =+ d=(rub c a)
44 [(add 2 p.d) (need (~(get by m) q.d)) m]
40 [(add 2 p.d) (need (~(get by m) q.d)) m]
45− ::
46−
47
41
48−Examples
42+#### Examples
49−--------
50
43
51 > (cue 12)
44 > (cue 12)
52 1
45 1
46+
53 > (cue 817)
47 > (cue 817)
54 [1 1]
48 [1 1]
49+
55 > (cue 4.657)
50 > (cue 4.657)
56 [1 2]
51 [1 2]
52+
57 > (cue 39.689)
53 > (cue 39.689)
58 [0 19]
54 [0 19]
59
55
@@ -63,29 +59,26 @@ Examples
63
59
64Pack noun to atom
60Pack noun to atom
65
61
66−Produces an atom packed from noun `a`. The inverse of cue.
62+Produces an atom packed from noun `a`. The inverse of `cue`.
67
63
68−Accepts
64+#### Accepts
69−-------
70
65
71`a` is a noun.
66`a` is a noun.
72
67
73−Produces
68+#### Produces
74−--------
75
69
76An atom.
70An atom.
77
71
78−Source
72+#### Source
79−------
80
73
81− ++ jam :: pack
74+ ++ jam
82 ~/ %jam
75 ~/ %jam
83− |= a/*
76+ |= a=*
84 ^- @
77 ^- @
85 =+ b=0
78 =+ b=0
86 =+ m=`(map * @)`~
79 =+ m=`(map * @)`~
87 =< q
80 =< q
88− |- ^- {p/@ q/@ r/(map * @)}
81+ |- ^- [p=@ q=@ r=(map * @)]
89 =+ c=(~(get by m) a)
82 =+ c=(~(get by m) a)
90 ?~ c
83 ?~ c
91 => .(m (~(put by m) a b))
84 => .(m (~(put by m) a b))
@@ -101,22 +94,25 @@ Source
101 [(add 1 p.d) (lsh 0 1 q.d) m]
94 [(add 1 p.d) (lsh 0 1 q.d) m]
102 =+ d=(mat u.c)
95 =+ d=(mat u.c)
103 [(add 2 p.d) (mix 3 (lsh 0 2 q.d)) m]
96 [(add 2 p.d) (mix 3 (lsh 0 2 q.d)) m]
104− ::
105
97
106
98
107−Examples
99+#### Examples
108−--------
109
100
110 > (jam 1)
101 > (jam 1)
111 12
102 12
103+
112 > (jam [1 1])
104 > (jam [1 1])
113 817
105 817
106+
114 > (jam [1 2])
107 > (jam [1 2])
115 4.657
108 4.657
109+
116 > (jam [~ u=19])
110 > (jam [~ u=19])
117 39.689
111 39.689
118
112
113+#### Discussion
119
114
115+`mat` is only used internally as a helper to `jam`.
120
116
121***
117***
122### `++mat`
118### `++mat`
@@ -127,49 +123,50 @@ Produces a cell whose tail `q` is atom `a` with a bit representation of
127its length prepended to it (as the least significant bits). The head `p`
123its length prepended to it (as the least significant bits). The head `p`
128is the length of `q` in bits.
124is the length of `q` in bits.
129
125
130−Accepts
126+#### Accepts
131−-------
132
127
133`a` is an atom.
128`a` is an atom.
134
129
135−Produces
130+#### Produces
136−--------
137
131
138−A cell of two atoms, `p` and `q`.
132+A cell of two atoms.
139
133
140−Source
134+#### Source
141−------
142
135
143− ++ mat :: length-encode
136+ ++ mat
144 ~/ %mat
137 ~/ %mat
145− |= a/@
138+ |= a=@
146− ^- {p/@ q/@}
139+ ^- [p=@ q=@]
147 ?: =(0 a)
140 ?: =(0 a)
148 [1 1]
141 [1 1]
149 =+ b=(met 0 a)
142 =+ b=(met 0 a)
150 =+ c=(met 0 b)
143 =+ c=(met 0 b)
151 :- (add (add c c) b)
144 :- (add (add c c) b)
152 (cat 0 (bex c) (mix (end 0 (dec c) b) (lsh 0 (dec c) a)))
145 (cat 0 (bex c) (mix (end 0 (dec c) b) (lsh 0 (dec c) a)))
153− ::
154−
155
146
156−Examples
147+#### Examples
157−--------
158
148
159 > (mat 0xaaa)
149 > (mat 0xaaa)
160 [p=20 q=699.024]
150 [p=20 q=699.024]
151+
161 > (met 0 q:(mat 0xaaa))
152 > (met 0 q:(mat 0xaaa))
162 20
153 20
154+
163 > `@ub`q:(mat 0xaaa)
155 > `@ub`q:(mat 0xaaa)
164 0b1010.1010.1010.1001.0000
156 0b1010.1010.1010.1001.0000
157+
165 > =a =-(~&(- -) `@ub`0xaaa)
158 > =a =-(~&(- -) `@ub`0xaaa)
166 0b1010.1010.1010
159 0b1010.1010.1010
160+
167 > =b =-(~&(- -) `@ub`(xeb a))
161 > =b =-(~&(- -) `@ub`(xeb a))
168 0b1100
162 0b1100
163+
169 > =b =-(~&(- -) `@ub`(met 0 a))
164 > =b =-(~&(- -) `@ub`(met 0 a))
170 0b1100
165 0b1100
166+
171 > =c =-(~&(- -) (xeb b))
167 > =c =-(~&(- -) (xeb b))
172 4
168 4
169+
173 > [`@ub`a `@ub`(end 0 (dec c) b) `@ub`(bex c)]
170 > [`@ub`a `@ub`(end 0 (dec c) b) `@ub`(bex c)]
174 [0b1010.1010.1010 0b100 0b1.0000]
171 [0b1010.1010.1010 0b100 0b1.0000]
175
172
@@ -180,30 +177,27 @@ Examples
180
177
181Length-decode
178Length-decode
182
179
183−The inverse of `++mat`. Accepts a cell of index `a` and a bitstring `b`
180+The inverse of `mat`. Accepts a cell of index `a` and a bitstring `b`
184and produces the cell whose tail `q` is the decoded atom at index `a`
181and produces the cell whose tail `q` is the decoded atom at index `a`
185and whose head is the length of the encoded atom `q`, by which the
182and whose head is the length of the encoded atom `q`, by which the
186−offset `a` is advanced. Only used internally as a helper cue.
183+offset `a` is advanced.
187
184
188−Accepts
185+#### Accepts
189−-------
190
186
191`a` is an atom.
187`a` is an atom.
192
188
193`b` is a bitstring as an atom.
189`b` is a bitstring as an atom.
194
190
195−Produces
191+#### Produces
196−--------
197
192
198−A cell of two atoms, `p` and `q`.
193+A cell of two atoms.
199
194
200−Source
195+#### Source
201−------
202
196
203− ++ rub :: length-decode
197+ ++ rub
204 ~/ %rub
198 ~/ %rub
205− |= {a/@ b/@}
199+ |= [a=@ b=@]
206− ^- {p/@ q/@}
200+ ^- [p=@ q=@]
207 =+ ^= c
201 =+ ^= c
208 =+ [c=0 m=(met 0 b)]
202 =+ [c=0 m=(met 0 b)]
209 |- ?< (gth c m)
203 |- ?< (gth c m)
@@ -217,14 +211,20 @@ Source
217 [(add (add c c) e) (cut 0 [(add d (dec c)) e] b)]
211 [(add (add c c) e) (cut 0 [(add d (dec c)) e] b)]
218
212
219
213
220−Examples
214+#### Examples
221−--------
222
215
223 > `@ub`(jam 0xaaa)
216 > `@ub`(jam 0xaaa)
224 0b1.0101.0101.0101.0010.0000
217 0b1.0101.0101.0101.0010.0000
218+
225 > (rub 1 0b1.0101.0101.0101.0010.0000)
219 > (rub 1 0b1.0101.0101.0101.0010.0000)
226 [p=20 q=2.730]
220 [p=20 q=2.730]
221+
227 > `@ux`q:(rub 1 0b1.0101.0101.0101.0010.0000)
222 > `@ux`q:(rub 1 0b1.0101.0101.0101.0010.0000)
228 0xaaa
223 0xaaa
229
224
225+
226+#### Discussion
227+
228+`rub` is only used internally as a helper to `cue`.
229+
230***
230***