on d7dad8b help sign in
289 lines 5.0 kB history raw
1+++
2title = "2j: Jar and Jug Logic"
3weight = 13
4template = "doc.html"
5+++
7### `++ja`
9Jar engine
11```hoon
12 |_ a=(jar)
13```
15A container arm for `++jar` operation arms. A `++jar` is a `++map` of
16`++list`s. The contained arms inherit the sample jar.
18`a` is a jar.
20```
21> ~(. ja (my [a+1 b+2 ~]))
22< 2.ngd
23 [ a=?(%~ [n=[?(p=%a p=%b) q=@ud] l=nlr([p=?(%a %b) q=@ud]) r=nlr([p=?(%a %b) q=@ud])])
24 <123.zao 45.xig 1.pnw %140>
25 ]
26```
28---
30### `++get:ja`
32Grab value by key
34```hoon
35 ++ get :: gets list by key
36 |* b=*
37 =+ c=(~(get by a) b)
38 ?~(c ~ u.c)
39```
41#### Accepts
43`a` is a `++jar`.
45`b` is a key of the same type as the keys in `a`.
47#### Produces
49A list retrieved from jar `a` using the key `b`.
51Examples
53```
54> =l (my [['a' (ly 1 2 3 ~)] ['b' (ly 4 5 6 ~)]~])
55> l
56[n=[p='b' q=[i=4 t=~[5 6]]] l={} r={[p='a' q=[i=1 t=~[2 3]]]}]
58> (~(get ja l) 'a')
59[i=1 t=[i=2 t=~[3]]]
61> (~(get ja l) 'b')
62[i=4 t=[i=5 t=~[6]]]
64> (~(get ja l) 'c')
66```
68---
70### `++add:ja`
72Prepend to list
74#### Source
76```hoon
77 ++ add :: adds key-list pair
78 |* [b=* c=*]
79 =+ d=(get b)
80 (~(put by a) b [c d])
81```
83#### Accepts
85`a` is a jar.
87`b` is a key of the same type as the keys in `a`.
89`c` is a value of the same type as the values in `a`.
91#### Examples
93```
94> =l (my [['a' (ly 1 2 3 ~)] ['b' (ly 4 5 6 ~)]~])
95> l
96[n=[p='b' q=[i=4 t=~[5 6]]] l={} r={[p='a' q=[i=1 t=~[2 3]]]}]
97> (~(add ja l) 'a' 100)
98[ n=[p='b' q=[i=4 t=[i=5 t=~[6]]]]
99 l=~
100 r=[n=[p='a' q=[i=100 t=[i=1 t=~[2 3]]]] l=~ r=~]
102> (~(add ja l) 'a' 100)
103[n=[p='b' q=[i=4 t=[i=5 t=~[6]]]] l=~ r=[n=[p='a' q=[i=100 t=[i=1 t=~[2 3]]]] l=~ r=~]]
104> (~(add ja l) 'b' 7)
105[n=[p='b' q=[i=7 t=[i=4 t=~[5 6]]]] l=~ r=[n=[p='a' q=[i=1 t=[i=2 t=~[3]]]] l=~ r=~]]
106> (~(add ja l) 'c' 7)
107[ n=[p='b' q=[i=4 t=[i=5 t=~[6]]]]
108 l=~
109 r=[n=[p='c' q=[i=7 t=~]] l=[n=[p='a' q=[i=1 t=~[2 3]]] l={} r={}] r=~]
111> (~(add ja l) 'c' (ly 7 8 9 ~))
112mull-grow
113mull-nice
114```
116\*\*
118### `++ju`
120Jug operations
122```hoon
123 ++ ju :: jug engine
124 |/ a=(jug)
125```
127Container arm for jug operation arms. A `++jug` is a `++map` of
128`++sets`. The contained arms inherit its sample jug, `a`.
130`a` is a jug.
132```
133> ~(. ju (my [[a (sy 1 ~)] [b (sy 2 ~)] ~]))
134< 5.cws
135 [ a
136 ?(
137 %~
138 [ n=[p=@ub ?(q=%~ q=[?(n=@ud #!) l=nlr(?(@ud #!)) r=nlr(?(@ud #!))])]
139 l=nlr([p=@ub q=?(%~ [?(n=@ud #!) l=nlr(?(@ud #!)) r=nlr(?(@ud #!))])])
140 r=nlr([p=@ub q=?(%~ [?(n=@ud #!) l=nlr(?(@ud #!)) r=nlr(?(@ud #!))])])
141 ]
142 )
143 <123.zao 45.xig 1.pnw %140>
144 ]
145```
147### `++del:ju`
149Remove
151#### Source
153```hoon
154 ++ del :: del key-set pair
155 |* [b=* c=*]
156 ^+ a
157 =+ d=(get b)
158 =+ e=(~(del in d) c)
159 ?~ e
160 (~(del by a) b)
161 (~(put by a) b e)
162```
164Produces jug `a` with value `c` removed from set located at key `b`.
166`a` is a jug.
168`b` is a key of the same type as the keys in `a`.
170`c` is the value of the same type of the keys in `a` that is to be
171removed.
173```
174 > s
175 [[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
177 > (~(del ju s) 'a' 1)
178 {[p='a' q={3 2}] [p='b' q={5 4 6}]}
180 > (~(del ju s) 'c' 7)
181 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
182```
184---
186### `++get:ju`
188Retrieve set
190```hoon
191 ++ get :: gets set by key
192 |* b=*
193 =+ c=(~(get by a) b)
194 ?~(c ~ u.c)
195```
197Produces a set retrieved from jar `a` using key `b`.
199`a` is a jar.
201`b` is a key of the same type as the keys in `a`.
203```
204 > s
205 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
206 > (~(get ju s) 'a')
207 {1 3 2}
208 > (~(get ju s) 'b')
209 {5 4 6}
210 > (~(get ju s) 'c')
211 ~
212```
214---
216### `++has:ju`
218Check contents
220```hoon
221 ++ has :: existence check
222 |* [b=* c=*]
223 ^- ?
224 (~(has in (get b)) c)
225```
227Computes whether a value `c` exists within the set located at key `b`
228with jar `a`. Produces a loobean.
230`a` is a set.
232`b` is a key as a noun.
234`c` is a value as a noun.
236```
237 > s
238 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
239 > (~(has ju s) 'a' 3)
240 %.y
241 > (~(has ju s) 'b' 6)
242 %.y
243 > (~(has ju s) 'a' 7)
244 %.n
245 > (~(has jus s) 'c' 7)
246 ! -find-limb.jus
247 ! find-none
248 ! exit
249 > (~(has ju s) 'c' 7)
250 %.n
251```
253---
255### `++put:ju`
257Add key-set pair
259```hoon
260 ++ put :: add key-set pair
261 |* [b=* c=*]
262 ^+ a
263 =+ d=(get b)
264 (~(put by a) b (~(put in d) c))
265```
267Produces jar `a` with `c` added to the set value located at key `b`.
269`a` is a set.
271`b` is a key as a noun.
273`c` is a value.
275```
276 > s
277 [p='a' q=[1 3 2]] [p='b' q=[5 4 6]]]
279 > (~(put ju s) 'a' 7)
280 {[p='a' q={7 1 3 2}] [p='b' q={5 4 6}]}
282 > (~(put ju s) 'a' 1)
283 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
285 > (~(put ju s) 'c' 7)
286 {[p='a' q={1 3 2}] [p='c' q={7}] [p='b' q={5 4 6}]}
287```
289---