on 8564483 help sign in
589 lines 14 kB history raw
1---
2navhome: /docs
3---
6# 2i: map logic
7### `++by`
9Map operations
11 ++ by :: map engine
12 ~/ %by
13 |_ a/(map)
16Container arm for map operation arms. A map is a set of key, value
17pairs. The contained arms inherit it's sample map, `a`.
19`a` is a map.
21 > ~(. by (mo (limo [%a 1] [%b 2] ~)))
22 <19.irb [nlr([p={%a %b} q=@ud]) <414.rvm 101.jzo 1.ypj %164>]>
24------------------------------------------------------------------------
26### `+-all:by`
28Logical AND
30 +- all :: logical AND
31 ~/ %all
32 |* b/$-(* ?)
33 |- ^- ?
34 ?~ a
35 &
36 ?&((b q.n.a) $(a l.a) $(a r.a))
37 ::
40Computes the logical AND on the results of slamming every element in map
41`a` with gate `b`. Produces a loobean.
43`a` is a map.
45`b` is a wet gate.
47 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
48 > (~(all by b) |=(a/* ?@(a & |)))
49 %.n
50 > =a (mo `(list {@t @u})`[['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
51 > (~(all by a) |=(a/@ (lte a 6)))
52 %.y
53 > (~(all by a) |=(a/@ (lte a 4)))
54 %.n
56------------------------------------------------------------------------
58### `+-any:by`
60Logical OR
62 +- any :: logical OR
63 ~/ %any
64 |* b=$+(* ?)
65 |- ^- ?
66 ?~ a
67 |
68 ?|((b q.n.a) $(a l.a) $(a r.a))
70Computes the logical OR on the results of slamming every element with
71gate `b`. Produces a loobean.
73`a` is a map.
75`b` is a wet gate.
77 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
78 > (~(all by b) |=(a/* ?@(a & |)))
79 %.y
80 > =a (mo `(list {@t @u})`[['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
81 > (~(any by a) |=(a=@ (lte a 4)))
82 %.y
84------------------------------------------------------------------------
86### `+-del:by`
88Delete
90 +- del :: delete at key b
91 ~/ %del
92 |* b/*
93 |- ^+ a
94 ?~ a
95 ~
96 ?. =(b p.n.a)
97 ?: (gor b p.n.a)
98 [n.a $(a l.a) r.a]
99 [n.a l.a $(a r.a)]
100 |- ^- {$?($~ _a)}
101 ?~ l.a r.a
102 ?~ r.a l.a
103 ?: (vor p.n.l.a p.n.r.a)
104 [n.l.a l.l.a $(l.a r.l.a)]
105 [n.r.a $(r.a l.r.a) r.r.a]
106 ::
108Produces map `a` with the element located at key `b` removed.
110`a` is a map.
112`b` is a key as a noun.
114 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
115 > (~(del by b) `a`)
116 {[p=`b` q=[2 3]]}
118------------------------------------------------------------------------
120### `+-dig:by`
122Axis of key
124 +- dig :: axis of b key
125 |= b/*
126 =+ c=1
127 |- ^- (unit @)
128 ?~ a ~
129 ?: =(b p.n.a) [~ u=(peg c 2)]
130 ?: (gor b p.n.a)
131 $(a l.a, c (peg c 6))
132 $(a r.a, c (peg c 7))
133 ::
136Produce the axis of key `b` within map `a`.
138`a` is a map.
140`b` is a key as a noun.
142 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
143 > (~(dig by b) `b`)
144 [~ 2]
146------------------------------------------------------------------------
148### `+-gas:by`
150Concatenate
152 +- gas :: concatenate
153 ~/ %gas
154 |= b/(list _?>(?=(^ a) n.a))
155 |- ^+ a
156 ?~ b
157 a
158 $(b t.b, a (put i.b))
159 ::
162Insert a list of key-value pairs `b` into map `a`.
164`a` is a map.
166`b` is a list of cells of key-value nouns `p` and `q`.
168 > =a (mo `(list {@t *})`[[`a` 1] [`b` 2] ~])
169 > =b `(list {@t *})`[[`c` 3] [`d` 4] ~]
170 > (~(gas by a) b)
171 {[p=`d` q=4] [p=`a` q=1] [p=`c` q=3] [p=`b` q=2]}
173------------------------------------------------------------------------
175### `+-get:by`
177Grab unit value
179 +- get :: unit value by key
180 ~/ %get
181 |* b=*
182 |- ^- ?(~ [~ u=_?>(?=(^ a) q.n.a)])
183 ?~ a
184 ~
185 ?: =(b p.n.a)
186 [~ u=q.n.a]
187 ?: (gor b p.n.a)
188 $(a l.a)
189 $(a r.a)
191Produce the unit value of the value located at key `b` within map `a`.
193`a` is a map
195`b` is a key as a noun
197 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
198 > (~(get by b) `b`)
199 [~ [2 3]]
201------------------------------------------------------------------------
203### `+-got:by`
205Assert
207 +- got
208 |* b/*
209 (need (get b))
211Produce the value located at key `b` within map `a`. Crash if key `b`
212does not exist.
214`a` is a map.
216`b` is a key.
218 > =m (mo `(list {@t *})`[['a' 1] ['b' 2] ~])
219 > m
220 {[p='a' q=1] [p='b' q=2]}
221 > (~(get by m) 'a')
222 [~ 1]
223 > (~(got by m) 'a')
224 1
225 > (~(got by m) 'c')
226 ! exit
228------------------------------------------------------------------------
230### `+-has:by`
232Key existence check
234 +- has :: key existence check
235 ~/ %has
236 |* b/*
237 !=(~ (get b))
240Checks whether map `a` contains an element with key `b`, producing a
241loobean.
243`a` is a map.
245`b` is a key as a noun.
247 > =b (mo `(list {@t *})`[['a' 1] ['b' [2 3]] ~])
248 > (~(has by b) `b`)
249 %.y
250 > (~(has by b) `c`)
251 %.n
253------------------------------------------------------------------------
255### `+-int:by`
257Intersection
259 +- int :: intersection
260 ~/ %int
261 |* b/_a
262 |- ^+ a
263 ?~ b
264 ~
265 ?~ a
266 ~
267 ?: (vor p.n.a p.n.b)
268 ?: =(p.n.b p.n.a)
269 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
270 ?: (gor p.n.b p.n.a)
271 %- uni(a $(a l.a, b [n.b l.b ~])) $(b r.b)
272 %- uni(a $(a r.a, b [n.b ~ r.b])) $(b l.b)
273 ?: =(p.n.a p.n.b)
274 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
275 ?: (gor p.n.a p.n.b)
276 %- uni(a $(b l.b, a [n.a l.a ~])) $(a r.a)
277 %- uni(a $(b r.b, a [n.a ~ r.a])) $(a l.a)
278 ::
280Produces a map of the (key) intersection between two maps of the same
281type, `a` and `b`. If both maps have an identical key that point to
282different values, the element from map `b` is used.
284`a` is a map.
286`b` is a map.
288 > =n (mo `(list {@t *})`[['a' 1] ['c' 3] ~])
289 > n
290 {[p='a' q=1] [p='c' q=3]}
291 > m
292 {[p='a' q=1] [p='b' q=2]}
293 > (~(int by m) n)
294 {[p='a' q=1]}
295 ~ravpel-holber/try=> =p (mo `(list {@t *})`[['a' 2] ['b' 2] ~])
296 > p
297 {[p='a' q=2] [p='b' q=2]}
298 > (~(int by p) n)
299 {[p='a' q=2]}
300 > =q (mo `(list {@t *})`[['a' 2] ['c' 2] ~])
301 > q
302 {[p='a' q=2] [p='b' q=2]}
303 > (~(int by p) q)
304 {[p='a' q=2] [p='b' q=2]}
305 > =o (mo `(list {@t *})`[['c' 3] ['d' 4] ~])
306 > (~(int by m) o)
307 {}
310------------------------------------------------------------------------
312### `+-mar:by`
314Assert and Add
316 +- mar :: add with validation
317 |* [b=_?>(?=(^ a) p.n.a) c=(unit ,_?>(?=(^ a) q.n.a))]
318 ?~ c
319 (del b)
320 (put b u.c)
322Produces map `a` with the addition of a key-value pair, where the value
323is a nonempty unit.
325Accept a noun and a unit of a noun of the type of the map's keys and
326values, respectively. Validate that the value is not null and put the
327pair in the map. If the value is null, delete the key.
329XX This arm is broken, asana task 15186618346453
331 > m
332 {[p='a' q=1] [p='b' q=2]}
333 > (~(mar by m) 'c' (some 3))
334 ! -find-limb.n
335 ! find-none
336 ! exit
337 > (~(mar by m) 'c' ~)
338 ! -find-limb.n
339 ! find-none
340 ! exit
341 > (~(mar by m) 'b' ~)
342 ! -find-limb.n
343 ! find-none
344 ! exit
346------------------------------------------------------------------------
348### `+-put:by`
350Add key-value pair
352 +- put :: adds key-value pair
353 ~/ %put
354 |* {b/* c/*}
355 |- ^+ a
356 ?~ a
357 [[b c] ~ ~]
358 ?: =(b p.n.a)
359 ?: =(c q.n.a)
360 a
361 [[b c] l.a r.a]
362 ?: (gor b p.n.a)
363 =+ d=$(a l.a)
364 ?> ?=(^ d)
365 ?: (vor p.n.a p.n.d)
366 [n.a d r.a]
367 [n.d l.d [n.a r.d r.a]]
368 =+ d=$(a r.a)
369 ?> ?=(^ d)
370 ?: (vor p.n.a p.n.d)
371 [n.a l.a d]
372 [n.d [n.a l.a l.d] r.d]
373 ::
375Produces `a` with the addition of the key-value pair of `b` and `c`.
377`a` is a map.
379`b` is a key of the same type as the keys in `a`.
381`c` is a value of the same type of the values in `a`.
383 > m
384 {[p='a' q=1] [p='b' q=2]}
385 > (~(put by m) 'c' 3)
386 {[p='a' q=1] [p='c' q=3] [p='b' q=2]}
387 > (~(put by m) "zod" 26)
388 ! type-fail
389 ! exit
390 > (~(put by m) 'a' 2)
391 {[p='a' q=2] [p='b' q=2]}
393------------------------------------------------------------------------
395### `+-rep:by`
397 +- rep :: replace by product
398 |* b/_|=({* *} +<+)
399 |-
400 ?~ a +<+.b
401 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
402 ::
404Accumulate using gate from values in map
406XX interface changing.
408------------------------------------------------------------------------
410### `+-rib:by`
412 +- rib :: transform + product
413 |* {b/* c/$-(* *)}
414 |- ^+ [b a]
415 ?~ a [b ~]
416 =+ d=(c n.a b)
417 =. n.a +.d
418 =+ e=$(a l.a, b -.d)
419 =+ f=$(a r.a, b -.e)
420 [-.f [n.a +.e +.f]]
421 ::
423Replace values with accumulator
425XX interface changing, possibly disappearing
427------------------------------------------------------------------------
429### `+-run:by`
431Transform values
433 +- run :: apply gate to values
434 |* b/$-(* *)
435 |-
436 ?~ a a
437 [n=[p=p.n.a q=(b q.n.a)] l=$(a l.a) r=$(a r.a)]
439Iterates over every value in set `a` using gate `b`. Produces a map.
441`a` is a map.
443`b` is a wet gate.
445 > m
446 {[p='a' q=1] [p='b' q=2]}
447 > ^+(m (~(run by m) dec))
448 {[p='a' q=0] [p='b' q=1]}
449 > `(map @tas @t)`(~(run by m) (cury scot %ux))
450 {[p=%a q='0x1'] [p=%b q='0x2']}
452------------------------------------------------------------------------
454### `+-tap:by`
456Listify pairs
458 +- tap :: listify pairs
459 ~/ %tap
460 |= b/(list _?>(?=(^ a) n.a))
461 ^+ b
462 ?~ a
463 b
464 $(a r.a, b [n.a $(a l.a)])
465 ::
467Produces the list of all elements in map `a` that is prepended to list
468`b`, which is empty by default.
470`a` is a map.
472`b` is a list.
474 {[p='a' q=1] [p='b' q=2]}
475 > `*`m
476 [[98 2] [[97 1] 0 0] 0]
477 > (~(tap by m))
478 ~[[p='b' q=2] [p='a' q=1]]
479 > `*`(~(tap by m))
480 [[98 2] [97 1] 0]
482------------------------------------------------------------------------
484### `+-uni:by`
486Union
488 +- uni :: union, merge
489 ~/ %uni
490 |* b/_a
491 |- ^+ a
492 ?~ b
493 a
494 ?~ a
495 b
496 ?: (vor p.n.a p.n.b)
497 ?: =(p.n.b p.n.a)
498 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
499 ?: (gor p.n.b p.n.a)
500 $(a [n.a $(a l.a, b [n.b l.b ~]) r.a], b r.b)
501 $(a [n.a l.a $(a r.a, b [n.b ~ r.b])], b l.b)
502 ?: =(p.n.a p.n.b)
503 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
504 ?: (gor p.n.a p.n.b)
505 $(b [n.b $(b l.b, a [n.a l.a ~]) r.b], a r.a)
506 $(b [n.b l.b $(b r.b, a [n.a ~ r.a])], a l.a)
508Produces a map of the union between the keys of `a` and `b`. If `b`
509shares a key with `a`, the tuple from `a` is preserved.
511`a` is a map.
513`b` is a map.
515 > m
516 {[p='a' q=1] [p='b' q=2]}
517 > o
518 {[p='d' q=4] [p='c' q=3]}
519 > (~(uni by m) o)
520 {[p='d' q=4] [p='a' q=1] [p='c' q=3] [p='b' q=2]}
521 > (~(uni by m) ~)
522 {[p='a' q=1] [p='b' q=2]}
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=3]}
527 > =n (mo `(list ,[@t *])`[['a' 1] ['c' 9] ~])
528 > n
529 {[p='a' q=1] [p='c' q=9]}
530 > (~(uni by o) n)
531 {[p='d' q=4] [p='a' q=1] [p='c' q=9]}
533------------------------------------------------------------------------
535### `+-urn:by`
537Turn (with key)
539 +- urn :: apply gate to nodes
540 |* b/$-({* *} *)
541 |-
542 ?~ a ~
543 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)]
545Iterates over every value in map `a` using gate `b`, which accepts both
546the key and the value of each element as its sample.
548`a` is a map.
550`b` is a wet gate that accepts two nouns (a key and a value) and
551produces a noun (the new value).
553 > m
554 {[p='a' q=1] [p='b' q=2]}
555 > (~(urn by m) |=(a=[p=* q=*] q.a))
556 {[p='a' q=1] [p='b' q=2]}
557 > (~(urn by m) |=(a=[p=* q=*] 7))
558 {[p='a' q=7] [p='b' q=7]}
559 > (~(urn by m) |=(a=[p=* q=*] p.a))
560 {[p='a' q=97] [p='b' q=98]}
562------------------------------------------------------------------------
564### `+-wyt:by`
566Depth
568 +- wyt :: depth of map
569 |- ^- @
570 ?~(a 0 +((add $(a l.a) $(a r.a))))
572Produce the depth of the tree map `a`.
574`a` is a map.
576 > m
577 {[p='a' q=1] [p='b' q=2]}
578 > o
579 {[p='d' q=4] [p='c' q=3]}
580 > ~(wyt by m)
581 2
582 > ~(wyt by o)
583 2
584 > ~(wyt by (~(uni by m) o))
585 4
589***