on master help sign in
Merge pull request #283 from urbit/rmariani-patch-15

Update 2p

commitcbda13bf001df5b1153e729f3fb7d949973e6047 authorJoshua Reagan <joshuareagan@users.noreply.github.com> dateFri 8 Jun 2018, 13:28 (8 years ago) parentd51bc0c43e5032
1 file changed+57−57
docs/hoon/library/2p.md+57−57
docs/hoon/library/2p.md+57−57
@@ -8,29 +8,25 @@ navhome: /docs/
88
99Unpack atom to noun
1010
11−Produces a noun unpacked from atom `a`. The inverse of jam.
11+Produces a noun unpacked from atom `a`. The inverse of `jam`.
1212
13−Accepts
14−-------
13+#### Accepts
1514
16−`a` is an atom.
1715
18−Produces
19−--------
16+#### Produces
2017
21−A `++noun`.
18+A noun.
2219
23−Source
24−------
20+#### Source
2521
26− ++ cue :: unpack
22+ ++ cue
2723 ~/ %cue
28− |= a/@
24+ |= a=@
2925 ^- *
3026 =+ b=0
3127 =+ m=`(map @ *)`~
3228 =< q
33− |- ^- {p/@ q/* r/(map @ *)}
29+ |- ^- [p=@ q=* r=(map @ *)]
3430 ?: =(0 (cut 0 [b 1] a))
3531 =+ c=(rub +(b) a)
3632 [+(p.c) q.c (~(put by m) b q.c)]
@@ -42,18 +38,18 @@ Source
4238 [(add 2 (add p.u p.v)) w (~(put by r.v) b w)]
4339 =+ d=(rub c a)
4440 [(add 2 p.d) (need (~(get by m) q.d)) m]
45− ::
46−
4741
48−Examples
49−--------
42+#### Examples
5043
5144 > (cue 12)
5245 1
46+
5347 > (cue 817)
5448 [1 1]
49+
5550 > (cue 4.657)
5651 [1 2]
52+
5753 > (cue 39.689)
5854 [0 19]
5955
@@ -63,29 +59,26 @@ Examples
6359
6460Pack noun to atom
6561
66−Produces an atom packed from noun `a`. The inverse of cue.
62+Produces an atom packed from noun `a`. The inverse of `cue`.
6763
68−Accepts
69−-------
64+#### Accepts
7065
7166`a` is a noun.
7267
73−Produces
74−--------
68+#### Produces
7569
7670An atom.
7771
78−Source
79−------
72+#### Source
8073
81− ++ jam :: pack
74+ ++ jam
8275 ~/ %jam
83− |= a/*
76+ |= a=*
8477 ^- @
8578 =+ b=0
8679 =+ m=`(map * @)`~
8780 =< q
88− |- ^- {p/@ q/@ r/(map * @)}
81+ |- ^- [p=@ q=@ r=(map * @)]
8982 =+ c=(~(get by m) a)
9083 ?~ c
9184 => .(m (~(put by m) a b))
@@ -101,22 +94,25 @@ Source
10194 [(add 1 p.d) (lsh 0 1 q.d) m]
10295 =+ d=(mat u.c)
10396 [(add 2 p.d) (mix 3 (lsh 0 2 q.d)) m]
104− ::
10597
10698
107−Examples
108−--------
99+#### Examples
109100
110101 > (jam 1)
111102 12
103+
112104 > (jam [1 1])
113105 817
106+
114107 > (jam [1 2])
115108 4.657
109+
116110 > (jam [~ u=19])
117111 39.689
118112
113+#### Discussion
119114
115+`mat` is only used internally as a helper to `jam`.
120116
121117***
122118### `++mat`
@@ -127,49 +123,50 @@ Produces a cell whose tail `q` is atom `a` with a bit representation of
127123its length prepended to it (as the least significant bits). The head `p`
128124is the length of `q` in bits.
129125
130−Accepts
131−-------
126+#### Accepts
132127
133128`a` is an atom.
134129
135−Produces
136−--------
130+#### Produces
137131
138−A cell of two atoms, `p` and `q`.
132+A cell of two atoms.
139133
140−Source
141−------
134+#### Source
142135
143− ++ mat :: length-encode
136+ ++ mat
144137 ~/ %mat
145− |= a/@
146− ^- {p/@ q/@}
138+ |= a=@
139+ ^- [p=@ q=@]
147140 ?: =(0 a)
148141 [1 1]
149142 =+ b=(met 0 a)
150143 =+ c=(met 0 b)
151144 :- (add (add c c) b)
152145 (cat 0 (bex c) (mix (end 0 (dec c) b) (lsh 0 (dec c) a)))
153− ::
154−
155146
156−Examples
157−--------
147+#### Examples
158148
159149 > (mat 0xaaa)
160150 [p=20 q=699.024]
151+
161152 > (met 0 q:(mat 0xaaa))
162153 20
154+
163155 > `@ub`q:(mat 0xaaa)
164156 0b1010.1010.1010.1001.0000
157+
165158 > =a =-(~&(- -) `@ub`0xaaa)
166159 0b1010.1010.1010
160+
167161 > =b =-(~&(- -) `@ub`(xeb a))
168162 0b1100
163+
169164 > =b =-(~&(- -) `@ub`(met 0 a))
170165 0b1100
166+
171167 > =c =-(~&(- -) (xeb b))
172168 4
169+
173170 > [`@ub`a `@ub`(end 0 (dec c) b) `@ub`(bex c)]
174171 [0b1010.1010.1010 0b100 0b1.0000]
175172
@@ -180,30 +177,27 @@ Examples
180177
181178Length-decode
182179
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`
184181and produces the cell whose tail `q` is the decoded atom at index `a`
185182and 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.
187184
188−Accepts
189−-------
185+#### Accepts
190186
191187`a` is an atom.
192188
193189`b` is a bitstring as an atom.
194190
195−Produces
196−--------
191+#### Produces
197192
198−A cell of two atoms, `p` and `q`.
193+A cell of two atoms.
199194
200−Source
201−------
195+#### Source
202196
203− ++ rub :: length-decode
197+ ++ rub
204198 ~/ %rub
205− |= {a/@ b/@}
206− ^- {p/@ q/@}
199+ |= [a=@ b=@]
200+ ^- [p=@ q=@]
207201 =+ ^= c
208202 =+ [c=0 m=(met 0 b)]
209203 |- ?< (gth c m)
@@ -217,14 +211,20 @@ Source
217211 [(add (add c c) e) (cut 0 [(add d (dec c)) e] b)]
218212
219213
220−Examples
221−--------
214+#### Examples
222215
223216 > `@ub`(jam 0xaaa)
224217 0b1.0101.0101.0101.0010.0000
218+
225219 > (rub 1 0b1.0101.0101.0101.0010.0000)
226220 [p=20 q=2.730]
221+
227222 > `@ux`q:(rub 1 0b1.0101.0101.0101.0010.0000)
228223 0xaaa
229224
225+
226+#### Discussion
227+
228+`rub` is only used internally as a helper to `cue`.
229+
230230***