+++ title = "2j: Jar and Jug Logic" weight = 13 template = "doc.html" +++ ### `++ja` Jar engine ```hoon |_ a=(jar) ``` A container arm for `++jar` operation arms. A `++jar` is a `++map` of `++list`s. The contained arms inherit the sample jar. `a` is a jar. ``` > ~(. ja (my [a+1 b+2 ~])) < 2.ngd [ a=?(%~ [n=[?(p=%a p=%b) q=@ud] l=nlr([p=?(%a %b) q=@ud]) r=nlr([p=?(%a %b) q=@ud])]) <123.zao 45.xig 1.pnw %140> ] ``` --- ### `++get:ja` Grab value by key ```hoon ++ get :: gets list by key |* b=* =+ c=(~(get by a) b) ?~(c ~ u.c) ``` #### Accepts `a` is a `++jar`. `b` is a key of the same type as the keys in `a`. #### Produces A list retrieved from jar `a` using the key `b`. Examples ``` > =l (my [['a' (ly 1 2 3 ~)] ['b' (ly 4 5 6 ~)]~]) > l [n=[p='b' q=[i=4 t=~[5 6]]] l={} r={[p='a' q=[i=1 t=~[2 3]]]}] > (~(get ja l) 'a') [i=1 t=[i=2 t=~[3]]] > (~(get ja l) 'b') [i=4 t=[i=5 t=~[6]]] > (~(get ja l) 'c') ~ ``` --- ### `++add:ja` Prepend to list #### Source ```hoon ++ add :: adds key-list pair |* [b=* c=*] =+ d=(get b) (~(put by a) b [c d]) ``` #### Accepts `a` is a jar. `b` is a key of the same type as the keys in `a`. `c` is a value of the same type as the values in `a`. #### Examples ``` > =l (my [['a' (ly 1 2 3 ~)] ['b' (ly 4 5 6 ~)]~]) > l [n=[p='b' q=[i=4 t=~[5 6]]] l={} r={[p='a' q=[i=1 t=~[2 3]]]}] > (~(add ja l) 'a' 100) [ 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=~] ] > (~(add ja l) 'a' 100) [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=~]] > (~(add ja l) 'b' 7) [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=~]] > (~(add ja l) 'c' 7) [ n=[p='b' q=[i=4 t=[i=5 t=~[6]]]] l=~ r=[n=[p='c' q=[i=7 t=~]] l=[n=[p='a' q=[i=1 t=~[2 3]]] l={} r={}] r=~] ] > (~(add ja l) 'c' (ly 7 8 9 ~)) mull-grow mull-nice ``` \*\* ### `++ju` Jug operations ```hoon ++ ju :: jug engine |/ a=(jug) ``` Container arm for jug operation arms. A `++jug` is a `++map` of `++sets`. The contained arms inherit its sample jug, `a`. `a` is a jug. ``` > ~(. ju (my [[a (sy 1 ~)] [b (sy 2 ~)] ~])) < 5.cws [ a ?( %~ [ n=[p=@ub ?(q=%~ q=[?(n=@ud #!) l=nlr(?(@ud #!)) r=nlr(?(@ud #!))])] l=nlr([p=@ub q=?(%~ [?(n=@ud #!) l=nlr(?(@ud #!)) r=nlr(?(@ud #!))])]) r=nlr([p=@ub q=?(%~ [?(n=@ud #!) l=nlr(?(@ud #!)) r=nlr(?(@ud #!))])]) ] ) <123.zao 45.xig 1.pnw %140> ] ``` ### `++del:ju` Remove #### Source ```hoon ++ del :: del key-set pair |* [b=* c=*] ^+ a =+ d=(get b) =+ e=(~(del in d) c) ?~ e (~(del by a) b) (~(put by a) b e) ``` Produces jug `a` with value `c` removed from set located at key `b`. `a` is a jug. `b` is a key of the same type as the keys in `a`. `c` is the value of the same type of the keys in `a` that is to be removed. ``` > s [[p='a' q={1 3 2}] [p='b' q={5 4 6}]} > (~(del ju s) 'a' 1) {[p='a' q={3 2}] [p='b' q={5 4 6}]} > (~(del ju s) 'c' 7) {[p='a' q={1 3 2}] [p='b' q={5 4 6}]} ``` --- ### `++get:ju` Retrieve set ```hoon ++ get :: gets set by key |* b=* =+ c=(~(get by a) b) ?~(c ~ u.c) ``` Produces a set retrieved from jar `a` using key `b`. `a` is a jar. `b` is a key of the same type as the keys in `a`. ``` > s {[p='a' q={1 3 2}] [p='b' q={5 4 6}]} > (~(get ju s) 'a') {1 3 2} > (~(get ju s) 'b') {5 4 6} > (~(get ju s) 'c') ~ ``` --- ### `++has:ju` Check contents ```hoon ++ has :: existence check |* [b=* c=*] ^- ? (~(has in (get b)) c) ``` Computes whether a value `c` exists within the set located at key `b` with jar `a`. Produces a loobean. `a` is a set. `b` is a key as a noun. `c` is a value as a noun. ``` > s {[p='a' q={1 3 2}] [p='b' q={5 4 6}]} > (~(has ju s) 'a' 3) %.y > (~(has ju s) 'b' 6) %.y > (~(has ju s) 'a' 7) %.n > (~(has jus s) 'c' 7) ! -find-limb.jus ! find-none ! exit > (~(has ju s) 'c' 7) %.n ``` --- ### `++put:ju` Add key-set pair ```hoon ++ put :: add key-set pair |* [b=* c=*] ^+ a =+ d=(get b) (~(put by a) b (~(put in d) c)) ``` Produces jar `a` with `c` added to the set value located at key `b`. `a` is a set. `b` is a key as a noun. `c` is a value. ``` > s [p='a' q=[1 3 2]] [p='b' q=[5 4 6]]] > (~(put ju s) 'a' 7) {[p='a' q={7 1 3 2}] [p='b' q={5 4 6}]} > (~(put ju s) 'a' 1) {[p='a' q={1 3 2}] [p='b' q={5 4 6}]} > (~(put ju s) 'c' 7) {[p='a' q={1 3 2}] [p='c' q={7}] [p='b' q={5 4 6}]} ``` ---