on dc20522 help sign in
272 lines 4.7 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 ::
144```
146Produces jug `a` with value `c` removed from set located at key `b`.
148`a` is a jug.
150`b` is a key of the same type as the keys in `a`.
152`c` is the value of the same type of the keys in `a` that is to be
153removed.
155```
156 > s
157 [[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
159 > (~(del ju s) 'a' 1)
160 {[p='a' q={3 2}] [p='b' q={5 4 6}]}
162 > (~(del ju s) 'c' 7)
163 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
164```
166---
168### `+-get:ju`
170Retrieve set
172```hoon
173 +- get :: gets set by key
174 |* b=*
175 =+ c=(~(get by a) b)
176 ?~(c ~ u.c)
177 ::
178```
180Produces a set retrieved from jar `a` using key `b`.
182`a` is a jar.
184`b` is a key of the same type as the keys in `a`.
186```
187 > s
188 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
189 > (~(get ju s) 'a')
190 {1 3 2}
191 > (~(get ju s) 'b')
192 {5 4 6}
193 > (~(get ju s) 'c')
194 ~
195```
197---
198### `+-has:ju`
200Check contents
202```hoon
203 +- has :: existence check
204 |* [b=* c=*]
205 ^- ?
206 (~(has in (get b)) c)
207 ::
208```
210Computes whether a value `c` exists within the set located at key `b`
211with jar `a`. Produces a loobean.
213`a` is a set.
215`b` is a key as a noun.
217`c` is a value as a noun.
219```
220 > s
221 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
222 > (~(has ju s) 'a' 3)
223 %.y
224 > (~(has ju s) 'b' 6)
225 %.y
226 > (~(has ju s) 'a' 7)
227 %.n
228 > (~(has jus s) 'c' 7)
229 ! -find-limb.jus
230 ! find-none
231 ! exit
232 > (~(has ju s) 'c' 7)
233 %.n
234```
235---
237### `+-put:ju`
239Add key-set pair
241```hoon
242 +- put
243 |* {b=* c=*}
244 ^+ a
245 =+ d=(get b)
246 (~(put by a) b (~(put in d) c))
247```
249Produces jar `a` with `c` added to the set value located at key `b`.
251`a` is a set.
253`b` is a key as a noun.
255`c` is a value.
257```
258 > s
259 [p='a' q=[1 3 2]] [p='b' q=[5 4 6]]]
261 > (~(put ju s) 'a' 7)
262 {[p='a' q={7 1 3 2}] [p='b' q={5 4 6}]}
264 > (~(put ju s) 'a' 1)
265 {[p='a' q={1 3 2}] [p='b' q={5 4 6}]}
267 > (~(put ju s) 'c' 7)
268 {[p='a' q={1 3 2}] [p='c' q={7}] [p='b' q={5 4 6}]}
269```
272---