on b00decd help sign in
269 lines 4.6 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 (mo (limo a/"ho" b/"he" ~)))
22 <2.dgz [nlr([p={%a %b} q=""]) <414.fvk 101.jzo 1.ypj %164>]>
23```
25---
26### `++get:ja`
28Grab value by key
30```hoon
31 ++ get :: gets list by key
32 |* b=*
33 =+ c=(~(get by a) b)
34 ?~(c ~ u.c)
35```
37#### Accepts
39`a` is a `++jar`.
41`b` is a key of the same type as the keys in `a`.
43#### Produces
45A list retrieved from jar `a` using the key `b`.
47Examples
49```
50 > =l (mo `(list ,[@t (list ,@)])`[['a' `(list ,@)`[1 2 3 ~]] ['b' `(list ,@)`[4 5 6 ~]] ~])
51 > l
52 [[p='a' q=~[1 2 3]] [p='b' q=~[4 5 6]]}
54 > (~(get ja l) 'a')
55 ~[1 2 3]
57 > (~(get ja l) 'b')
58 ~[4 5 6]
60 > (~(get ja l) 'c')
61 ~
62```
64---
65### `++add:ja`
67Prepend to list
69#### Source
71```hoon
72 ++ add :: adds key-list pair
73 |* [b=* c=*]
74 =+ d=(get b)
75 (~(put by a) b [c d])
76```
78#### Accepts
80`a` is a jar.
82`b` is a key of the same type as the keys in `a`.
84`c` is a value of the same type as the values in `a`.
86#### Examples
88```
89 > =l (mo `(list ,[@t (list ,@)])`[['a' `(list ,@)`[1 2 3 ~]] ['b' `(list ,@)`[4 5 6 ~]] ~])
90 > l
91 {[p='a' q=~[1 2 3]] [p='b' q=~[4 5 6]]}
93 > (~(add ja l) 'b' 7)
94 {[p='a' q=~[1 2 3]] [p='b' q=~[7 4 5 6]]}
96 > (~(add ja l) 'a' 100)
97 {[p='a' q=~[100 1 2 3]] [p='b' q=~[4 5 6]]}
99 > (~(add ja l) 'c' 7)
100 {[p='a' q=~[1 2 3]] [p='c' q=~[7]] [p='b' q=~[4 5 6]]}
102 > (~(add ja l) 'c' `(list ,@)`[7 8 9 ~])
103 ! type-fail
104 ! exit
105```
107**
108### `++ju`
110Jug operations
112```hoon
113 ++ ju :: jug engine
114 |/ a=(jug)
115```
117Container arm for jug operation arms. A `++jug` is a `++map` of
118`++sets`. The contained arms inherit its sample jug, `a`.
120`a` is a jug.
122```
123 > ~(. ju (mo (limo a=(sa "ho") b=(sa "he") ~)))
124 <2.dgz [nlr([p={%a %b} q={nlr(^$1{@tD $1}) nlr(^$3{@tD $3})}]) <414.fvk 101.jzo 1.ypj %164>]>
125```
127### `++del:ju`
129Remove
132#### Source
134```hoon
135 ++ del :: del key-set pair
136 |* [b=* c=*]
137 ^+ a
138 =+ d=(get b)
139 =+ e=(~(del in d) c)
140 ?~ e
141 (~(del by a) b)
142 (~(put by a) b e)
143```
145Produces jug `a` with value `c` removed from set located at key `b`.
147`a` is a jug.
149`b` is a key of the same type as the keys in `a`.
151`c` is the value of the same type of the keys in `a` that is to be
152removed.
154```
155 > s
156 [[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
158 > (~(del ju s) 'a' 1)
159 {[p='a' q={3 2}] [p='b' q={5 4 6}]}
161 > (~(del ju s) 'c' 7)
162 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
163```
165---
167### `++get:ju`
169Retrieve set
171```hoon
172 ++ get :: gets set by key
173 |* b=*
174 =+ c=(~(get by a) b)
175 ?~(c ~ u.c)
176```
178Produces a set retrieved from jar `a` using key `b`.
180`a` is a jar.
182`b` is a key of the same type as the keys in `a`.
184```
185 > s
186 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
187 > (~(get ju s) 'a')
188 {1 3 2}
189 > (~(get ju s) 'b')
190 {5 4 6}
191 > (~(get ju s) 'c')
192 ~
193```
195---
196### `++has:ju`
198Check contents
200```hoon
201 ++ has :: existence check
202 |* [b=* c=*]
203 ^- ?
204 (~(has in (get b)) c)
205```
207Computes whether a value `c` exists within the set located at key `b`
208with jar `a`. Produces a loobean.
210`a` is a set.
212`b` is a key as a noun.
214`c` is a value as a noun.
216```
217 > s
218 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
219 > (~(has ju s) 'a' 3)
220 %.y
221 > (~(has ju s) 'b' 6)
222 %.y
223 > (~(has ju s) 'a' 7)
224 %.n
225 > (~(has jus s) 'c' 7)
226 ! -find-limb.jus
227 ! find-none
228 ! exit
229 > (~(has ju s) 'c' 7)
230 %.n
231```
232---
234### `++put:ju`
236Add key-set pair
238```hoon
239 ++ put :: add key-set pair
240 |* [b=* c=*]
241 ^+ a
242 =+ d=(get b)
243 (~(put by a) b (~(put in d) c))
244```
246Produces jar `a` with `c` added to the set value located at key `b`.
248`a` is a set.
250`b` is a key as a noun.
252`c` is a value.
254```
255 > s
256 [p='a' q=[1 3 2]] [p='b' q=[5 4 6]]]
258 > (~(put ju s) 'a' 7)
259 {[p='a' q={7 1 3 2}] [p='b' q={5 4 6}]}
261 > (~(put ju s) 'a' 1)
262 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
264 > (~(put ju s) 'c' 7)
265 {[p='a' q={1 3 2}] [p='c' q={7}] [p='b' q={5 4 6}]}
266```
269---