on 140ac0e help sign in
584 lines 14 kB history raw
1# 2i: map logic
2### `++by`
4Map operations
6 ++ by :: map engine
7 ~/ %by
8 |_ a/(map)
11Container arm for map operation arms. A map is a set of key, value
12pairs. The contained arms inherit it's sample map, `a`.
14`a` is a map.
16 > ~(. by (mo (limo [%a 1] [%b 2] ~)))
17 <19.irb [nlr([p={%a %b} q=@ud]) <414.rvm 101.jzo 1.ypj %164>]>
19------------------------------------------------------------------------
21### `+-all:by`
23Logical AND
25 +- all :: logical AND
26 ~/ %all
27 |* b/$-(* ?)
28 |- ^- ?
29 ?~ a
30 &
31 ?&((b q.n.a) $(a l.a) $(a r.a))
32 ::
35Computes the logical AND on the results of slamming every element in map
36`a` with gate `b`. Produces a loobean.
38`a` is a map.
40`b` is a wet gate.
42 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
43 > (~(all by b) |=(a/* ?@(a & |)))
44 %.n
45 > =a (mo `(list {@t @u})`[['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
46 > (~(all by a) |=(a/@ (lte a 6)))
47 %.y
48 > (~(all by a) |=(a/@ (lte a 4)))
49 %.n
51------------------------------------------------------------------------
53### `+-any:by`
55Logical OR
57 +- any :: logical OR
58 ~/ %any
59 |* b=$+(* ?)
60 |- ^- ?
61 ?~ a
62 |
63 ?|((b q.n.a) $(a l.a) $(a r.a))
65Computes the logical OR on the results of slamming every element with
66gate `b`. Produces a loobean.
68`a` is a map.
70`b` is a wet gate.
72 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
73 > (~(all by b) |=(a/* ?@(a & |)))
74 %.y
75 > =a (mo `(list {@t @u})`[['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
76 > (~(any by a) |=(a=@ (lte a 4)))
77 %.y
79------------------------------------------------------------------------
81### `+-del:by`
83Delete
85 +- del :: delete at key b
86 ~/ %del
87 |* b/*
88 |- ^+ a
89 ?~ a
90 ~
91 ?. =(b p.n.a)
92 ?: (gor b p.n.a)
93 [n.a $(a l.a) r.a]
94 [n.a l.a $(a r.a)]
95 |- ^- {$?($~ _a)}
96 ?~ l.a r.a
97 ?~ r.a l.a
98 ?: (vor p.n.l.a p.n.r.a)
99 [n.l.a l.l.a $(l.a r.l.a)]
100 [n.r.a $(r.a l.r.a) r.r.a]
101 ::
103Produces map `a` with the element located at key `b` removed.
105`a` is a map.
107`b` is a key as a noun.
109 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
110 > (~(del by b) `a`)
111 {[p=`b` q=[2 3]]}
113------------------------------------------------------------------------
115### `+-dig:by`
117Axis of key
119 +- dig :: axis of b key
120 |= b/*
121 =+ c=1
122 |- ^- (unit @)
123 ?~ a ~
124 ?: =(b p.n.a) [~ u=(peg c 2)]
125 ?: (gor b p.n.a)
126 $(a l.a, c (peg c 6))
127 $(a r.a, c (peg c 7))
128 ::
131Produce the axis of key `b` within map `a`.
133`a` is a map.
135`b` is a key as a noun.
137 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
138 > (~(dig by b) `b`)
139 [~ 2]
141------------------------------------------------------------------------
143### `+-gas:by`
145Concatenate
147 +- gas :: concatenate
148 ~/ %gas
149 |= b/(list _?>(?=(^ a) n.a))
150 |- ^+ a
151 ?~ b
152 a
153 $(b t.b, a (put i.b))
154 ::
157Insert a list of key-value pairs `b` into map `a`.
159`a` is a map.
161`b` is a list of cells of key-value nouns `p` and `q`.
163 > =a (mo `(list {@t *})`[[`a` 1] [`b` 2] ~])
164 > =b `(list {@t *})`[[`c` 3] [`d` 4] ~]
165 > (~(gas by a) b)
166 {[p=`d` q=4] [p=`a` q=1] [p=`c` q=3] [p=`b` q=2]}
168------------------------------------------------------------------------
170### `+-get:by`
172Grab unit value
174 +- get :: unit value by key
175 ~/ %get
176 |* b=*
177 |- ^- ?(~ [~ u=_?>(?=(^ a) q.n.a)])
178 ?~ a
179 ~
180 ?: =(b p.n.a)
181 [~ u=q.n.a]
182 ?: (gor b p.n.a)
183 $(a l.a)
184 $(a r.a)
186Produce the unit value of the value located at key `b` within map `a`.
188`a` is a map
190`b` is a key as a noun
192 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
193 > (~(get by b) `b`)
194 [~ [2 3]]
196------------------------------------------------------------------------
198### `+-got:by`
200Assert
202 +- got
203 |* b/*
204 (need (get b))
206Produce the value located at key `b` within map `a`. Crash if key `b`
207does not exist.
209`a` is a map.
211`b` is a key.
213 > =m (mo `(list {@t *})`[['a' 1] ['b' 2] ~])
214 > m
215 {[p='a' q=1] [p='b' q=2]}
216 > (~(get by m) 'a')
217 [~ 1]
218 > (~(got by m) 'a')
219 1
220 > (~(got by m) 'c')
221 ! exit
223------------------------------------------------------------------------
225### `+-has:by`
227Key existence check
229 +- has :: key existence check
230 ~/ %has
231 |* b/*
232 !=(~ (get b))
235Checks whether map `a` contains an element with key `b`, producing a
236loobean.
238`a` is a map.
240`b` is a key as a noun.
242 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
243 > (~(has by b) `b`)
244 %.y
245 > (~(has by b) `c`)
246 %.n
248------------------------------------------------------------------------
250### `+-int:by`
252Intersection
254 +- int :: intersection
255 ~/ %int
256 |* b/_a
257 |- ^+ a
258 ?~ b
259 ~
260 ?~ a
261 ~
262 ?: (vor p.n.a p.n.b)
263 ?: =(p.n.b p.n.a)
264 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
265 ?: (gor p.n.b p.n.a)
266 %- uni(a $(a l.a, b [n.b l.b ~])) $(b r.b)
267 %- uni(a $(a r.a, b [n.b ~ r.b])) $(b l.b)
268 ?: =(p.n.a p.n.b)
269 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
270 ?: (gor p.n.a p.n.b)
271 %- uni(a $(b l.b, a [n.a l.a ~])) $(a r.a)
272 %- uni(a $(b r.b, a [n.a ~ r.a])) $(a l.a)
273 ::
275Produces a map of the (key) intersection between two maps of the same
276type, `a` and `b`. If both maps have an identical key that point to
277different values, the element from map `b` is used.
279`a` is a map.
281`b` is a map.
283 > =n (mo `(list {@t *})`[['a' 1] ['c' 3] ~])
284 > n
285 {[p='a' q=1] [p='c' q=3]}
286 > m
287 {[p='a' q=1] [p='b' q=2]}
288 > (~(int by m) n)
289 {[p='a' q=1]}
290 ~ravpel-holber/try=> =p (mo `(list {@t *})`[['a' 2] ['b' 2] ~])
291 > p
292 {[p='a' q=2] [p='b' q=2]}
293 > (~(int by p) n)
294 {[p='a' q=2]}
295 > =q (mo `(list {@t *})`[['a' 2] ['c' 2] ~])
296 > q
297 {[p='a' q=2] [p='b' q=2]}
298 > (~(int by p) q)
299 {[p='a' q=2] [p='b' q=2]}
300 > =o (mo `(list {@t *})`[['c' 3] ['d' 4] ~])
301 > (~(int by m) o)
302 {}
305------------------------------------------------------------------------
307### `+-mar:by`
309Assert and Add
311 +- mar :: add with validation
312 |* [b=_?>(?=(^ a) p.n.a) c=(unit ,_?>(?=(^ a) q.n.a))]
313 ?~ c
314 (del b)
315 (put b u.c)
317Produces map `a` with the addition of a key-value pair, where the value
318is a nonempty unit.
320Accept a noun and a unit of a noun of the type of the map's keys and
321values, respectively. Validate that the value is not null and put the
322pair in the map. If the value is null, delete the key.
324XX This arm is broken, asana task 15186618346453
326 > m
327 {[p='a' q=1] [p='b' q=2]}
328 > (~(mar by m) 'c' (some 3))
329 ! -find-limb.n
330 ! find-none
331 ! exit
332 > (~(mar by m) 'c' ~)
333 ! -find-limb.n
334 ! find-none
335 ! exit
336 > (~(mar by m) 'b' ~)
337 ! -find-limb.n
338 ! find-none
339 ! exit
341------------------------------------------------------------------------
343### `+-put:by`
345Add key-value pair
347 +- put :: adds key-value pair
348 ~/ %put
349 |* {b/* c/*}
350 |- ^+ a
351 ?~ a
352 [[b c] ~ ~]
353 ?: =(b p.n.a)
354 ?: =(c q.n.a)
355 a
356 [[b c] l.a r.a]
357 ?: (gor b p.n.a)
358 =+ d=$(a l.a)
359 ?> ?=(^ d)
360 ?: (vor p.n.a p.n.d)
361 [n.a d r.a]
362 [n.d l.d [n.a r.d r.a]]
363 =+ d=$(a r.a)
364 ?> ?=(^ d)
365 ?: (vor p.n.a p.n.d)
366 [n.a l.a d]
367 [n.d [n.a l.a l.d] r.d]
368 ::
370Produces `a` with the addition of the key-value pair of `b` and `c`.
372`a` is a map.
374`b` is a key of the same type as the keys in `a`.
376`c` is a value of the same type of the values in `a`.
378 > m
379 {[p='a' q=1] [p='b' q=2]}
380 > (~(put by m) 'c' 3)
381 {[p='a' q=1] [p='c' q=3] [p='b' q=2]}
382 > (~(put by m) "zod" 26)
383 ! type-fail
384 ! exit
385 > (~(put by m) 'a' 2)
386 {[p='a' q=2] [p='b' q=2]}
388------------------------------------------------------------------------
390### `+-rep:by`
392 +- rep :: replace by product
393 |* b/_|=({* *} +<+)
394 |-
395 ?~ a +<+.b
396 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
397 ::
399Accumulate using gate from values in map
401XX interface changing.
403------------------------------------------------------------------------
405### `+-rib:by`
407 +- rib :: transform + product
408 |* {b/* c/$-(* *)}
409 |- ^+ [b a]
410 ?~ a [b ~]
411 =+ d=(c n.a b)
412 =. n.a +.d
413 =+ e=$(a l.a, b -.d)
414 =+ f=$(a r.a, b -.e)
415 [-.f [n.a +.e +.f]]
416 ::
418Replace values with accumulator
420XX interface changing, possibly disappearing
422------------------------------------------------------------------------
424### `+-run:by`
426Transform values
428 +- run :: apply gate to values
429 |* b/$-(* *)
430 |-
431 ?~ a a
432 [n=[p=p.n.a q=(b q.n.a)] l=$(a l.a) r=$(a r.a)]
434Iterates over every value in set `a` using gate `b`. Produces a map.
436`a` is a map.
438`b` is a wet gate.
440 > m
441 {[p='a' q=1] [p='b' q=2]}
442 > ^+(m (~(run by m) dec))
443 {[p='a' q=0] [p='b' q=1]}
444 > `(map @tas @t)`(~(run by m) (cury scot %ux))
445 {[p=%a q='0x1'] [p=%b q='0x2']}
447------------------------------------------------------------------------
449### `+-tap:by`
451Listify pairs
453 +- tap :: listify pairs
454 ~/ %tap
455 |= b/(list _?>(?=(^ a) n.a))
456 ^+ b
457 ?~ a
458 b
459 $(a r.a, b [n.a $(a l.a)])
460 ::
462Produces the list of all elements in map `a` that is prepended to list
463`b`, which is empty by default.
465`a` is a map.
467`b` is a list.
469 {[p='a' q=1] [p='b' q=2]}
470 > `*`m
471 [[98 2] [[97 1] 0 0] 0]
472 > (~(tap by m))
473 ~[[p='b' q=2] [p='a' q=1]]
474 > `*`(~(tap by m))
475 [[98 2] [97 1] 0]
477------------------------------------------------------------------------
479### `+-uni:by`
481Union
483 +- uni :: union, merge
484 ~/ %uni
485 |* b/_a
486 |- ^+ a
487 ?~ b
488 a
489 ?~ a
490 b
491 ?: (vor p.n.a p.n.b)
492 ?: =(p.n.b p.n.a)
493 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
494 ?: (gor p.n.b p.n.a)
495 $(a [n.a $(a l.a, b [n.b l.b ~]) r.a], b r.b)
496 $(a [n.a l.a $(a r.a, b [n.b ~ r.b])], b l.b)
497 ?: =(p.n.a p.n.b)
498 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
499 ?: (gor p.n.a p.n.b)
500 $(b [n.b $(b l.b, a [n.a l.a ~]) r.b], a r.a)
501 $(b [n.b l.b $(b r.b, a [n.a ~ r.a])], a l.a)
503Produces a map of the union between the keys of `a` and `b`. If `b`
504shares a key with `a`, the tuple from `a` is preserved.
506`a` is a map.
508`b` is a map.
510 > m
511 {[p='a' q=1] [p='b' q=2]}
512 > o
513 {[p='d' q=4] [p='c' q=3]}
514 > (~(uni by m) o)
515 {[p='d' q=4] [p='a' q=1] [p='c' q=3] [p='b' q=2]}
516 > (~(uni by m) ~)
517 {[p='a' q=1] [p='b' q=2]}
518 > n
519 {[p='a' q=1] [p='c' q=9]}
520 > (~(uni by o) n)
521 {[p='d' q=4] [p='a' q=1] [p='c' q=3]}
522 > =n (mo `(list ,[@t *])`[['a' 1] ['c' 9] ~])
523 > n
524 {[p='a' q=1] [p='c' q=9]}
525 > (~(uni by o) n)
526 {[p='d' q=4] [p='a' q=1] [p='c' q=9]}
528------------------------------------------------------------------------
530### `+-urn:by`
532Turn (with key)
534 +- urn :: apply gate to nodes
535 |* b/$-({* *} *)
536 |-
537 ?~ a ~
538 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)]
540Iterates over every value in map `a` using gate `b`, which accepts both
541the key and the value of each element as its sample.
543`a` is a map.
545`b` is a wet gate that accepts two nouns (a key and a value) and
546produces a noun (the new value).
548 > m
549 {[p='a' q=1] [p='b' q=2]}
550 > (~(urn by m) |=(a=[p=* q=*] q.a))
551 {[p='a' q=1] [p='b' q=2]}
552 > (~(urn by m) |=(a=[p=* q=*] 7))
553 {[p='a' q=7] [p='b' q=7]}
554 > (~(urn by m) |=(a=[p=* q=*] p.a))
555 {[p='a' q=97] [p='b' q=98]}
557------------------------------------------------------------------------
559### `+-wyt:by`
561Depth
563 +- wyt :: depth of map
564 |- ^- @
565 ?~(a 0 +((add $(a l.a) $(a r.a))))
567Produce the depth of the tree map `a`.
569`a` is a map.
571 > m
572 {[p='a' q=1] [p='b' q=2]}
573 > o
574 {[p='d' q=4] [p='c' q=3]}
575 > ~(wyt by m)
576 2
577 > ~(wyt by o)
578 2
579 > ~(wyt by (~(uni by m) o))
580 4
584***