on 4fdc19d help sign in
397 lines 6.0 kB history raw
1+++
2title = "2j: Jar and Jug Logic"
3weight = 13
4template = "doc.html"
5+++
7## `++ja`
9Jar engine
11A container arm for `jar` operation arms. A `jar` is a `map` of
12`list`s. The contained arms inherit the sample jar.
14#### Accepts
16`a` is a jar.
18#### Produces
20A core.
22#### Source
24```hoon
25++ ja
26 =| a=(tree (pair * (list))) :: (jar)
27 |@
28```
30#### Examples
32```
33> ~(. ja (my [a+1 b+2 ~]))
34< 2.ngd
35 [ a
36 ?(
37 %~
38 [ n=[?(p=%a p=%b) q=@ud]
39 l=nlr([p=?(%a %b) q=@ud])
40 r=nlr([p=?(%a %b) q=@ud])
41 ]
42 )
43 <123.zao 46.hgz 1.pnw %140>
44 ]
46```
48---
50### `++get:ja`
52Grab value by key
54Produces the list at key `b` in jar `a`.
56#### Accepts
58`a` is a `jar`, and is the sample of `+ja`.
60`b` is a noun.
62#### Produces
64A `list`.
66#### Source
68```hoon
69++ get
70 |* b=*
71 =+ c=(~(get by a) b)
72 ?~(c ~ u.c)
73```
75#### Examples
77```
78> =j `(jar @t @ud)`(malt ~[['a' `(list @ud)`~[1 2 3]] ['b' `(list @ud)`~[4 5 6]]])
79> j
80{[p='b' q=~[4 5 6]] [p='a' q=~[1 2 3]]}
82> `(list @ud)`(~(get ja j) 'a')
83~[1 2 3]
85> `(list @ud)`(~(get ja j) 'b')
86~[4 5 6]
88> `(list @ud)`(~(get ja j) 'c')
90```
92---
94### `++add:ja`
96Prepend to list
98Adds `c` to the head of the list at key `b` in jar `a`. If `b` does not exist in `a`, a new key-value pair is added with a list containing `c`.
100#### Accepts
102`a` is a `jar`, and is the sample of `+ja`.
104`b` is a noun of the same type as the keys in `a`.
106`c` is a noun of the same type the lists in `a` contain.
108#### Produces
110A `jar`.
112#### Source
114```hoon
115++ add
116 |* [b=* c=*]
117 =+ d=(get b)
118 (~(put by a) b [c d])
119```
121#### Examples
123```
124> =j `(jar @t @ud)`(malt ~[['a' `(list @ud)`~[1 2 3]] ['b' `(list @ud)`~[4 5 6]]])
125> j
126{[p='b' q=~[4 5 6]] [p='a' q=~[1 2 3]]}
128> `(jar @t @ud)`(~(add ja j) 'b' 7)
129{[p='b' q=~[7 4 5 6]] [p='a' q=~[1 2 3]]}
131> `(jar @t @ud)`(~(add ja j) 'c' 8)
132{[p='b' q=~[4 5 6]] [p='a' q=~[1 2 3]] [p='c' q=~[8]]}
133```
135---
137## `++ju`
139Jug operations
141Container arm for jug operation arms. A `jug` is a `map` of
142`set`s. The contained arms inherit its sample jug, `a`.
144#### Accepts
146`a` is a `jug`.
148#### Produces
150A core.
152#### Source
154```hoon
155++ ju
156 =| a=(tree (pair * (tree))) :: (jug)
157 |@
158```
160#### Example
162```
163> =j `(jug @t @ud)`(malt ~[['a' (silt ~[1 2 3])] ['b' (silt ~[4 5 6])]])
164> j
165{[p='b' q={5 6 4}] [p='a' q={1 2 3}]}
167> ~(. ju j)
168<5.cws [a=nlr([p=@t q=nlr(@ud)]) <123.zao 46.hgz 1.pnw %140>]>
169```
171---
173### `++del:ju`
175Remove
177Produces jug `a` with value `c` removed from set located at key `b`.
179#### Accepts
181`a` is a jug, and is the sample of `+ju`.
183`b` is a noun of the same type as the keys in `a`.
185`c` is a noun of the same type as the sets in `a` contain.
187#### Source
189```hoon
190++ del
191 |* [b=* c=*]
192 ^+ a
193 =+ d=(get b)
194 =+ e=(~(del in d) c)
195 ?~ e
196 (~(del by a) b)
197 (~(put by a) b e)
198```
200#### Examples
202```
203> j
204{[p='b' q={5 6 4}] [p='a' q={1 2 3}]}
206> `(jug @t @ud)`(~(del ju j) 'b' 6)
207{[p='b' q={5 4}] [p='a' q={1 2 3}]}
209> `(jug @t @ud)`(~(del ju j) 'b' 10)
210{[p='b' q={5 6 4}] [p='a' q={1 2 3}]}
212> `(jug @t @ud)`(~(del ju j) 'c' 10)
213{[p='b' q={5 6 4}] [p='a' q={1 2 3}]}
214```
216---
218### `++gas:ju`
220Concatenate
222Add each of the key-value pairs in list `b` to jug `a`. The values in `b` are the type the sets in `a` contain. For keys in `b` that exist in `a`, the values will be added to their sets. For keys in `b` that don't exist in `a`, new keys and sets will be added.
224#### Accepts
226`a` is a `jug`, and is the sample of `+ju`.
228`b` is a `(list [p q])`, where:
230- `p` is a noun, the type of the keys in `a`.
231- `q` is a noun, the type the sets in `a` contain.
233#### Produces
235A `jug`.
237#### Source
239```hoon
240++ gas
241 |* b=(list [p=* q=*])
242 => .(b `(list _?>(?=([[* ^] ^] a) [p=p q=n.q]:n.a))`b)
243 |- ^+ a
244 ?~ b
245 a
246 $(b t.b, a (put p.i.b q.i.b))
247```
249#### Examples
251```
252> =j `(jug @t @ud)`(malt ~[['a' (silt ~[1 2 3])] ['b' (silt ~[4 5 6])]])
253> j
254{[p='b' q={5 6 4}] [p='a' q={1 2 3}]}
256> `(jug @t @ud)`(~(gas ju j) ~[['a' 10] ['a' 42] ['b' 999] ['c' 7]])
257{[p='b' q={5 6 4 999}] [p='a' q={10 42 1 2 3}] [p='c' q={7}]}
258```
260---
262### `++get:ju`
264Retrieve set
266Produces a set retrieved from jug `a` using key `b`.
268#### Accepts
270`a` is a jug, and the sample of `+ju`.
272`b` is key, a noun of the same type as the keys in `a`.
274#### Produces
276A `set`.
278#### Source
280```hoon
281++ get
282 |* b=*
283 =+ c=(~(get by a) b)
284 ?~(c ~ u.c)
285```
287#### Examples
289```
290> =j `(jug @t @ud)`(malt ~[['a' (silt ~[1 2 3])] ['b' (silt ~[4 5 6])]])
291> j
292{[p='b' q={5 6 4}] [p='a' q={1 2 3}]}
294> `(set @ud)`(~(get ju j) 'a')
295{1 2 3}
297> `(set @ud)`(~(get ju j) 'b')
298{5 6 4}
300> `(set @ud)`(~(get ju j) 'c')
301{}
302```
304---
306### `++has:ju`
308Check contents
310Computes whether a value `c` exists within the set located at key `b`
311with jug `a`, producing a flag.
313#### Accepts
315`a` is a `jug`, and the sample of `+ju`.
317`b` is a noun of the same type as the keys in `a`.
319`c` is a noun of the same type as the sets in `a` contain.
321#### Produces
323A `?`.
325#### Source
327```hoon
328++ has
329 |* [b=* c=*]
330 ^- ?
331 (~(has in (get b)) c)
332```
334#### Examples
336```
337> =j `(jug @t @ud)`(malt ~[['a' (silt ~[1 2 3])] ['b' (silt ~[4 5 6])]])
338> j
339{[p='b' q={5 6 4}] [p='a' q={1 2 3}]}
341> (~(has ju j) 'b' 5)
342%.y
344> (~(has ju j) 'b' 10)
345%.n
347> (~(has ju j) 'c' 10)
348%.n
349```
351---
353### `++put:ju`
355Add key-set pair
357Produces jug `a` with `c` added to the set located at key `b`. If `b` isn't a key in `a`, it will be added and a new set created containing `c`.
359#### Accepts
361`a` is a `jug`.
363`b` is a noun, the type of the keys in `a`.
365`c` is a noun, the type the sets in `a` contain.
367#### Produces
369A `jug`.
371#### Source
373```hoon
374++ put
375 |* [b=* c=*]
376 ^+ a
377 =+ d=(get b)
378 (~(put by a) b (~(put in d) c))
379```
381#### Examples
383```
384> j
385{[p='b' q={5 6 4}] [p='a' q={1 2 3}]}
387> `(jug @t @ud)`(~(put ju j) 'c' 5)
388{[p='b' q={5 6 4}] [p='a' q={1 2 3}] [p='c' q={5}]}
390> `(jug @t @ud)`(~(put ju j) 'a' 4)
391{[p='b' q={5 6 4}] [p='a' q={1 2 3 4}]}
393> `(jug @t @ud)`(~(put ju j) 'a' 1)
394{[p='b' q={5 6 4}] [p='a' q={1 2 3}]}
395```
397---