on c19bc7a help sign in
1062 lines 15 kB history raw
1:- :~ navhome/'/docs/'
2 sort/'12'
3 title/'2i: Map Logic'
4 ==
6;>
8# 2i: map logic
10### `++by`
12Map operations
14Container arm for map operation arms. A map is a set of key-value pairs. The
15contained arms inherit its sample map, `a`.
17### Accepts
19`a` is a map.
21#### Source
23```
24 ++ by
25 ~/ %by
26 =| a=(tree (pair))
27 =* node ?>(?=(^ a) n.a)
28```
30#### Examples
32```
33 > ~(. by (my [%a 1] [%b 2] ~))
34 < 27.zjr
35 { a
36 ?(
37 { n/{?(p/$a p/$b) q/@ud}
38 l/nlr({p/?($b $a) q/@ud})
39 r/nlr({p/?($b $a) q/@ud})
40 }
41 $~
42 )
43 <119.wim 31.ohr 1.jmk $143>
44 }
45```
47---
48### `+-all:by`
50Logical AND
52Computes the logical AND on the results of slamming every element in map `a`
53with gate `b`.
55#### Accepts
57`a` is a map.
59`b` is a wet gate.
61#### Produces
63A flag.
65#### Source
67```
68 +- all
69 ~/ %all
70 |* b=$-(* ?)
71 |- ^- ?
72 ?~ a
73 &
74 ?&((b q.n.a) $(a l.a) $(a r.a))
75```
77#### Examples
79```
80 > =b (my [['a' 1] ['b' [2 3]] ~])
81 > (~(all by b) |=(a=* ?@(a & |)))
82 %.n
84 > =a (my [['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
85 > (~(all by a) |=(a=@ (lte a 6)))
86 %.y
88 > (~(all by a) |=(a=@ (lte a 4)))
89 %.n
90```
92---
93### `+-any:by`
95Logical OR
97Computes the logical OR on the results of slamming every element with
98gate `b`.
100#### Accepts
102`a` is a map.
104`b` is a wet gate.
106#### Produces
108A flag.
110#### Source
112```
113 +- any
114 ~/ %any
115 |* b=$-(* ?)
116 |- ^- ?
117 ?~ a
118 |
119 ?|((b q.n.a) $(a l.a) $(a r.a))
120```
122#### Examples
124```
125 > =b (my [['a' 1] ['b' [2 3]] ~])
126 > (~(any by b) |=(a=* ?@(a & |)))
127 %.y
129 > =a (my [['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
130 > (~(any by a) |=(a=@ (lte a 4)))
131 %.y
132```
134---
135###`+-apt:by`
137Check correctness
139Computes whether `a` has a correct horizontal order and a correct vertical
140order, producing a flag.
142#### Accepts
144`a` is a map.
146#### Produces
148A flag.
150#### Source
152```
153 +- apt
154 =| [l=(unit) r=(unit)]
155 |- ^- ?
156 ?~ a &
157 ?& ?~(l & (gor p.n.a u.l))
158 ?~(r & (gor u.r p.n.a))
159 ?~(l.a & ?&((vor p.n.a p.n.l.a) $(a l.a, l `p.n.a)))
160 ?~(r.a & ?&((vor p.n.a p.n.r.a) $(a r.a, r `p.n.a)))
161 ==
162```
164#### Examples
166```
167 > =a (my [['a' 1] ['b' [2 3]] ~])
168 > ~(apt by a)
169 %.y
171 > =s (my [[1 2] [3 4] ~])
172 > =z ?~ s ~ s(p.n 10)
173 > z
174 [n=[p=10 q=2] l={} r={[p=3 q=4]}]
176 > ~(apt by z)
177 %.n
178```
180#### Discussion
182See section [`2f`](../2f) for more information on noun ordering.
184###`+-bif:by`
186Bifurcate
188Splits map `a` into two maps `l` and `r`, with the head at `b` discarded.
190#### Accepts
192`a` is a map.
194`b` is a noun.
196`c` is a noun.
198#### Produces
200A cell of two maps.
202#### Source
204```
205 +- bif
206 ~/ %bif
207 |* [b=* c=*]
208 ^+ [l=a r=a]
209 =< +
210 |- ^+ a
211 ?~ a
212 [[b c] ~ ~]
213 ?: =(b p.n.a)
214 ?: =(c q.n.a)
215 a
216 [[b c] l.a r.a]
217 ?: (gor b p.n.a)
218 =+ d=$(a l.a)
219 ?> ?=(^ d)
220 [n.d l.d [n.a r.d r.a]]
221 =+ d=$(a r.a)
222 ?> ?=(^ d)
223 [n.d [n.a l.a l.d] r.d]
224```
226#### Examples
228```
229 > =a (my [['a' 1] ['b' 2] ['c' 3] ~])
231 > (~(bif by a) ['b' 2])
232 [l=[n=[p='c' q=3] l=[n=[p='a' q=1] l={} r={}] r=~] r=~]
233```
235###`+-def:by`
237Difference
239#### Accepts
241`a` is a map.
243`b` is a map.
245#### Produces
247A map.
249#### Source
251```
252 +- def
253 |* b=_a
254 ^- (map _p:node (pair (unit _q:node) (unit _q:node)))
255 !!
256```
258#### Examples
260---
261###`+-dep:by`
263Difference as patch
266#### Accepts
268`a` is a map.
270`b` is a map.
272#### Produces
274A cell of maps.
276#### Source
278```
279 +- dep
280 |* b=_a
281 ^+ [p=a q=a]
282 =+ c=~(tap by (def b))
283 =+ [d e]=[`_a`~ `_a`~]
284 |- ^+ [d e]
285 ?~ c [d e]
286 %= $
287 c t.c
288 d ?~(q.q.i.c d (~(put by d) p.i.c u.q.q.i.c))
289 e ?~(p.q.i.c e (~(put by e) p.i.c u.p.q.i.c))
290 ==
291```
293#### Examples
297---
298### `+-del:by`
300Delete
302Produces map `a` with the element located at key `b` removed.
304#### Accepts
306`a` is a map.
308`b` is a key as a noun.
310#### Produces
312A map.
314#### Source
316```
317 +- del
318 ~/ %del
319 |* b=*
320 |- ^+ a
321 ?~ a
322 ~
323 ?. =(b p.n.a)
324 ?: (gor b p.n.a)
325 [n.a $(a l.a) r.a]
326 [n.a l.a $(a r.a)]
327 |- ^- [(~ _a)]
328 ?~ l.a r.a
329 ?~ r.a l.a
330 ?: (vor p.n.l.a p.n.r.a)
331 [n.l.a l.l.a $(l.a r.l.a)]
332 [n.r.a $(r.a l.r.a) r.r.a]
333```
335#### Examples
337```
338 > =b (my [['a' 1] ['b' [2 3]] ~])
339 > (~(del by b) 'a')
340 [n=[p='b' q=[2 3]] l=~ r=~]
341```
343---
344### `+-dig:by`
346Address of key
348Produce the address of key `b` within map `a`.
350#### Accepts
352`a` is a map.
354`b` is a key as a noun.
356#### Produces
358A unit.
360#### Source
362```
363 +- dig
364 |= b=*
365 =+ c=1
366 |- ^- (unit @)
367 ?~ a ~
368 ?: =(b p.n.a) [~ u=(peg c 2)]
369 ?: (gor b p.n.a)
370 $(a l.a, c (peg c 6))
371 $(a r.a, c (peg c 7))
372```
374#### Examples
376```
377 > =b (my [['a' 1] ['b' [2 3]] ~])
378 > (~(dig by b) 'b')
379 [~ u=2]
380```
382---
383### `+-gas:by`
385Concatenate
387Insert a list of key-value pairs `b` into map `a`.
389#### Accepts
391`a` is a map.
393`b` is a list of cells of key-value nouns `p` and `q`.
395#### Produces
397A map.
399#### Source
401```
402 +- gas
403 ~/ %gas
404 |* b=(list [p=* q=*])
405 => .(b `(list _?>(?=(^ a) n.a))`b)
406 |- ^+ a
407 ?~ b
408 a
409 $(b t.b, a (put p.i.b q.i.b))
410```
412#### Examples
414```
415 > =a (my [['a' 1] ['b' 2] ~])
416 > =b [['c' 3] ['d' 4] ~]
418 > (~(gas by a) b)
419 [n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]
420```
422---
423### `+-get:by`
425Grab unit value
427Produce the unit value of the value located at key `b` within map `a`.
429#### Accepts
431`a` is a map.
433`b` is a key as a noun.
435#### Produces
437A unit.
439#### Source
441```
442 +- get
443 ~/ %get
444 |= b=*
445 ^- [$@(~ [~ u=_?>(?=(^ a) q.n.a)])]
446 =+ 42
447 ?~ a
448 ~
449 ?: =(b p.n.a)
450 [~ u=q.n.a]
451 ?: (gor b p.n.a)
452 $(a l.a)
453 $(a r.a)
454```
456#### Examples
458```
459 > =b (my [['a' 1] ['b' [2 3]] ~])
460 > (~(get by b) 'b')
461 [~ u=[2 3]]
464 > =m (my [['a' 1] ['b' 2] ~])
465 > (~(get by m) 'a')
466 [~ u=1]
467```
469---
470### `+-got:by`
472Assert
474Produce the value located at key `b` within map `a`. Crash if key `b` does not
475exist.
477#### Accepts
479`a` is a map.
481`b` is a key as a noun.
483#### Produces
485A noun.
487#### Source
489```
490 +- got
491 |* b=*
492 (need (get b))
493```
495#### Examples
497```
498 > =m (my [['a' 1] ['b' 2] ~])
499 > m
500 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
502 > (~(got by m) 'a')
503 1
505 > (~(got by m) 'c')
506 ! exit
507```
509---
510### `+-has:by`
512Key existence check
514Checks whether map `a` contains an element with key `b`, producing a flag.
516#### Accepts
518`a` is a map.
520`b` is a key as a noun.
522#### Produces
524A flag.
526#### Source
528```
529 +- has
530 ~/ %has
531 |* b=*
532 !=(~ (get b))
533```
535#### Examples
537```
538 > =b (my [['a' 1] ['b' [2 3]] ~])
539 > (~(has by b) 'b')
540 %.y
542 > (~(has by b) 'c')
543 %.n
544```
546---
547### `+-int:by`
549Intersection
551Produces a map of the (key) intersection between two maps of the same type, `a`
552and `b`. If both maps have an identical key that point to different values, the
553element from map `b` is used.
555#### Accepts
557`a` is a map.
559`b` is a map.
561#### Produces
563A map.
565#### Source
567```
568 +- int
569 ~/ %int
570 |* b=_a
571 |- ^+ a
572 ?~ b
573 ~
574 ?~ a
575 ~
576 ?: (vor p.n.a p.n.b)
577 ?: =(p.n.b p.n.a)
578 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
579 ?: (gor p.n.b p.n.a)
580 %- uni(a $(a l.a, b [n.b l.b ~])) $(b r.b)
581 %- uni(a $(a r.a, b [n.b ~ r.b])) $(b l.b)
582 ?: =(p.n.a p.n.b)
583 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
584 ?: (gor p.n.a p.n.b)
585 %- uni(a $(b l.b, a [n.a l.a ~])) $(a r.a)
586 %- uni(a $(b r.b, a [n.a ~ r.a])) $(a l.a)
587```
589#### Examples
591```
592 > =n (my [['a' 1] ['c' 3] ~])
593 > =m (my [['a' 1] ['b' 2] ~])
595 > (~(int by m) n)
596 [n=[p='a' q=1] l=~ r=~]
598 > =p (my [['a' 2] ['b' 2] ~])
599 > p
600 [n=[p='b' q=2] l={[p='a' q=2]} r={}]
601 > (~(int by p) n)
602 [n=[p='a' q=1] l=~ r=~]
604 > =q (my [['a' 2] ['c' 2] ~])
605 > q
606 [n=[p='c' q=2] l={[p='a' q=2]} r={}]
607 > (~(int by p) q)
608 [n=[p='a' q=2] l=~ r=~]
610 > =o (my [['c' 3] ['d' 4] ~])
611 > (~(int by m) o)
612 ~
613```
615---
616### `+-mar:by`
618Assert and add
620Produces map `a` with the addition of a key-value pair, where the value is a
621nonempty unit.
623Accept a noun and a unit of a noun of the type of the map's keys and values,
624respectively. Validate that the value is not null and put the pair in the map.
625If the value is null, delete the key.
627#### Accepts
629`a` is a map.
631`b` is a unit.
633#### Produces
635A map.
637#### Assert and Add
639```
640 +- mar
641 |* [b=_?>(?=(^ a) p.n.a) c=(unit ,_?>(?=(^ a) q.n.a))]
642 ?~ c
643 (del b)
644 (put b u.c)
645```
647#### Examples
649```
650 > =m (my [['a' 1] ['b' 2] ~])
651 > (~(mar by m) 'c' (some 3))
652 [n=[p='b' q=2] l=[n=[p='c' q=3] l=[n=[p='a' q=1] l={} r={}] r=~] r=~]
654 > (~(mar by m) 'c' ~)
655 [n=[p='b' q=2] l=[n=[p='a' q=1] l=~ r=~] r=~]
657 > (~(mar by m) 'b' ~)
658 [n=[p='a' q=1] l=~ r=~]
659```
661---
662### `+-put:by`
664Add key-value pair
666Produces `a` with the addition of the key-value pair of `b` and `c`.
668#### Accepts
670`a` is a map.
672`b` is a key of the same type as the keys in `a`.
674`c` is a value of the same type of the values in `a`.
676#### Produces
678A map.
680#### Source
682```
683 +- put
684 ~/ %put
685 |* [b=* c=*]
686 |- ^+ a
687 ?~ a
688 [[b c] ~ ~]
689 ?: =(b p.n.a)
690 ?: =(c q.n.a)
691 a
692 [[b c] l.a r.a]
693 ?: (gor b p.n.a)
694 =+ d=$(a l.a)
695 ?> ?=(^ d)
696 ?: (vor p.n.a p.n.d)
697 [n.a d r.a]
698 [n.d l.d [n.a r.d r.a]]
699 =+ d=$(a r.a)
700 ?> ?=(^ d)
701 ?: (vor p.n.a p.n.d)
702 [n.a l.a d]
703 [n.d [n.a l.a l.d] r.d]
704```
706#### Examples
708```
709 > =m (my [['a' 1] ['b' 2] ~])
710 > (~(put by ~) 'c' 3)
711 [n=[p='b' q=2] l=[n=[p='c' q=3] l={[p='a' q=1]} r={}] r=~]
713 > (~(put by m) "zod" 26)
714 ! nest-fail
715 ! exit
717 > (~(put by m) 'a' 2)
718 [n=[p='b' q=2] l=[n=[p='a' q=2] l={} r={}] r=~]
720 > (put:by m 'a' 2)
721 {[p=[[98 2] [[97 1] 0 0] 0] q=[97 2]]}
722```
724---
725### `+-rep:by`
727Replace by product
729Accumulate elements of map `a` using gate `b`, producing a noun.
731#### Accepts
733`a` is a map.
735`b` is a wet gate.
737#### Produces
739A noun.
741#### Source
743```
744 +- rep
745 |* b=_|=([* *] +<+)
746 |-
747 ?~ a +<+.b
748 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
749```
751#### Examples
753```
754 > =m (my [['a' 1] ['b' 2] ~])
755 > (~(rep by m) |=(a=(pair * *) p.a))
756 q=[97 1]
757```
759---
760### `+-rib:by`
762Replace values with accumulator
764#### Accepts
766`a` is a map.
768`b` is a noun.
770`c` is a wet gate.
772####
774A cell of nouns.
776#### Source
778```
779 +- rib
780 |* [b=* c=gate]
781 |- ^+ [b a]
782 ?~ a [b ~]
783 =+ d=(c n.a b)
784 =. n.a +.d
785 =+ e=$(a l.a, b -.d)
786 =+ f=$(a r.a, b -.e)
787 [-.f [n.a +.e +.f]]
788```
790#### Examples
792```
793 > =m (my [['a' 1] ['b' 2] ~])
794 > (~(rib by m) [1 2] (con by m))
796 > (~(rep by m) [1 2] |=(a=(pair * *) p.a))
797```
799---
800### `+-run:by`
802Transform values
804Iterates over every value in map `a` using gate `b`, producing a map.
806#### Accepts
808`a` is a map.
810`b` is a wet gate.
812#### Produces
814A map.
816#### Source
818```
819 +- run
820 |* b=gate
821 |-
822 ?~ a a
823 [n=[p=p.n.a q=(b q.n.a)] l=$(a l.a) r=$(a r.a)]
824```
826#### Examples
828```
829 > =m (my [['a' 1] ['b' 2] ~])
831 > ^+(m (~(run by m) dec))
832 [n=[p='b' q=1] l={[p='a' q=0]} r={}]
834 > `(map @tas @t)`(~(run by m) (cury scot %ux))
835 {[p=%a q='0x1'] [p=%b q='0x2']}
836```
838---
839### `+-rut:by`
841Transform nodes
843Applies a gate `b` to nodes in map `a`.
845#### Accepts
847`a` is a map.
849`b` is a wet gate.
851#### Produces
853A map.
855#### Source
857```
858 +- rut
859 |* b=gate
860 |-
861 ?~ a a
862 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)]
863```
865#### Examples
867```
868 > =m (my [['a' 1] ['b' 2] ~])
869```
871---
872### `+-tap:by`
874Listify pairs
876Produces the list of all elements in map `a` that is prepended to list `b`,
877which is empty by default.
879#### Accepts
881`a` is a map.
883`b` is a list.
885#### Produces
887A list.
889#### Source
891```
892 +- tap
893 =< $
894 ~/ %tap
895 =| b=(list _?>(?=(^ a) n.a))
896 |. ^+ b
897 ?~ a
898 b
899 $(a r.a, b [n.a $(a l.a)])
900```
902#### Examples
904```
905 > =m (my [['a' 1] ['b' 2] ~])
906 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
907 > `*`m
908 [[98 2] [[97 1] 0 0] 0]
910 > ~(tap by m)
911 ~[[p='b' q=2] [p='a' q=1]]
912 > `*`~(tap by m)
913 [[98 2] [97 1] 0]
914```
916---
917### `+-uni:by`
919Union
921Produces a map of the union between the keys of `a` and `b`. If `b` shares a
922key with `a`, the tuple from `b` is preserved.
924#### Accepts
926`a` is a map.
928`b` is a map.
930#### Produces
932A map.
934#### Source
936```
937 +- uni
938 ~/ %uni
939 |* b=_a
940 |- ^+ a
941 ?~ b
942 a
943 ?~ a
944 b
945 ?: (vor p.n.a p.n.b)
946 ?: =(p.n.b p.n.a)
947 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
948 ?: (gor p.n.b p.n.a)
949 $(a [n.a $(a l.a, b [n.b l.b ~]) r.a], b r.b)
950 $(a [n.a l.a $(a r.a, b [n.b ~ r.b])], b l.b)
951 ?: =(p.n.a p.n.b)
952 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
953 ?: (gor p.n.a p.n.b)
954 $(b [n.b $(b l.b, a [n.a l.a ~]) r.b], a r.a)
955 $(b [n.b l.b $(b r.b, a [n.a ~ r.a])], a l.a)
956```
958#### Examples
960```
961 > =m (my [['a' 1] ['b' 2] ~])
963 > =o (my [['c' 3] ['d' 4] ~])
965 > (~(uni by m) o)
966 [n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]
968 > (~(uni by m) ~)
969 {[p='a' q=1] [p='b' q=2]}
971 > n
972 {[p='a' q=1] [p='c' q=3]}
973 > (~(uni by o) n)
974 {[p='d' q=4] [p='a' q=1] [p='c' q=3]}
975```
977---
978### `+-urn:by`
980Turn (with key)
982Iterates over every value in map `a` using gate `b`, which accepts both
983the key and the value of each element as its sample.
985#### Accepts
987`a` is a map.
989`b` is a wet gate that accepts two nouns.
991#### Produces
993A noun.
995#### Source
997```
998 +- urn
999 |* b=$-({* *} *)
1000 |-
1001 ?~ a ~
1002 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)
1003```
1005#### Examples
1007```
1008 > m
1009 {[p='a' q=1] [p='b' q=2]}
1011 > (~(urn by m) |=(a=(pair * *) q.a))
1012 {[p='a' q=1] [p='b' q=2]}
1014 > (~(urn by m) |=(a=(pair * *) 7))
1015 {[p='a' q=7] [p='b' q=7]}
1017 > (~(urn by m) |=(a=(pair * *) p.a))
1018 {[p='a' q=97] [p='b' q=98]}
1019```
1021---
1023### `+-wyt:by`
1025Depth
1027Produce the depth of the tree map `a`.
1029#### Accepts
1031`a` is a map.
1033#### Produces
1035An atom.
1037#### Source
1039```
1040 +- wyt
1041 |- ^- @
1042 ?~(a 0 +((add $(a l.a) $(a r.a))))
1043```
1045#### Examples
1047```
1048 > =m (my [['a' 1] ['b' 2] ~])
1050 > =o (my [['c' 3] ['d' 4] ~])
1052 > ~(wyt by m)
1055 > ~(wyt by o)
1058 > ~(wyt by (~(uni by m) o))
1060```
1062---