on b93c19b help sign in
1058 lines 15 kB history raw
1+++
2title = "2i: Map Logic"
3weight = 12
4template = "doc.html"
5+++
6### `++by`
8Map operations
10Container arm for map operation arms. A map is a set of key-value pairs. The
11contained arms inherit its sample map, `a`.
13### Accepts
15`a` is a map.
17#### Source
19```
20 ++ by
21 ~/ %by
22 =| a=(tree (pair))
23 =* node ?>(?=(^ a) n.a)
24```
26#### Examples
28```
29 > ~(. by (my [%a 1] [%b 2] ~))
30 < 27.zjr
31 { a
32 ?(
33 { n/{?(p/$a p/$b) q/@ud}
34 l/nlr({p/?($b $a) q/@ud})
35 r/nlr({p/?($b $a) q/@ud})
36 }
37 $~
38 )
39 <119.wim 31.ohr 1.jmk $143>
40 }
41```
43---
44### `+-all:by`
46Logical AND
48Computes the logical AND on the results of slamming every element in map `a`
49with gate `b`.
51#### Accepts
53`a` is a map.
55`b` is a wet gate.
57#### Produces
59A flag.
61#### Source
63```
64 +- all
65 ~/ %all
66 |* b=$-(* ?)
67 |- ^- ?
68 ?~ a
69 &
70 ?&((b q.n.a) $(a l.a) $(a r.a))
71```
73#### Examples
75```
76 > =b (my [['a' 1] ['b' [2 3]] ~])
77 > (~(all by b) |=(a=* ?@(a & |)))
78 %.n
80 > =a (my [['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
81 > (~(all by a) |=(a=@ (lte a 6)))
82 %.y
84 > (~(all by a) |=(a=@ (lte a 4)))
85 %.n
86```
88---
89### `+-any:by`
91Logical OR
93Computes the logical OR on the results of slamming every element with
94gate `b`.
96#### Accepts
98`a` is a map.
100`b` is a wet gate.
102#### Produces
104A flag.
106#### Source
108```
109 +- any
110 ~/ %any
111 |* b=$-(* ?)
112 |- ^- ?
113 ?~ a
114 |
115 ?|((b q.n.a) $(a l.a) $(a r.a))
116```
118#### Examples
120```
121 > =b (my [['a' 1] ['b' [2 3]] ~])
122 > (~(any by b) |=(a=* ?@(a & |)))
123 %.y
125 > =a (my [['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
126 > (~(any by a) |=(a=@ (lte a 4)))
127 %.y
128```
130---
131### `+-apt:by`
133Check correctness
135Computes whether `a` has a correct horizontal order and a correct vertical
136order, producing a flag.
138#### Accepts
140`a` is a map.
142#### Produces
144A flag.
146#### Source
148```
149 +- apt
150 =| [l=(unit) r=(unit)]
151 |- ^- ?
152 ?~ a &
153 ?& ?~(l & (gor p.n.a u.l))
154 ?~(r & (gor u.r p.n.a))
155 ?~(l.a & ?&((vor p.n.a p.n.l.a) $(a l.a, l `p.n.a)))
156 ?~(r.a & ?&((vor p.n.a p.n.r.a) $(a r.a, r `p.n.a)))
157 ==
158```
160#### Examples
162```
163 > =a (my [['a' 1] ['b' [2 3]] ~])
164 > ~(apt by a)
165 %.y
167 > =s (my [[1 2] [3 4] ~])
168 > =z ?~ s ~ s(p.n 10)
169 > z
170 [n=[p=10 q=2] l={} r={[p=3 q=4]}]
172 > ~(apt by z)
173 %.n
174```
176#### Discussion
178See section [`2f`](@/docs/reference/library/2f.md) for more information on noun ordering.
180### `+-bif:by`
182Bifurcate
184Splits map `a` into two maps `l` and `r`, with the head at `b` discarded.
186#### Accepts
188`a` is a map.
190`b` is a noun.
192`c` is a noun.
194#### Produces
196A cell of two maps.
198#### Source
200```
201 +- bif
202 ~/ %bif
203 |* [b=* c=*]
204 ^+ [l=a r=a]
205 =< +
206 |- ^+ a
207 ?~ a
208 [[b c] ~ ~]
209 ?: =(b p.n.a)
210 ?: =(c q.n.a)
211 a
212 [[b c] l.a r.a]
213 ?: (gor b p.n.a)
214 =+ d=$(a l.a)
215 ?> ?=(^ d)
216 [n.d l.d [n.a r.d r.a]]
217 =+ d=$(a r.a)
218 ?> ?=(^ d)
219 [n.d [n.a l.a l.d] r.d]
220```
222#### Examples
224```
225 > =a (my [['a' 1] ['b' 2] ['c' 3] ~])
227 > (~(bif by a) ['b' 2])
228 [l=[n=[p='c' q=3] l=[n=[p='a' q=1] l={} r={}] r=~] r=~]
229```
231### `+-def:by`
233Difference
235#### Accepts
237`a` is a map.
239`b` is a map.
241#### Produces
243A map.
245#### Source
247```
248 +- def
249 |* b=_a
250 ^- (map _p:node (pair (unit _q:node) (unit _q:node)))
251 !!
252```
254#### Examples
256---
257### `+-dep:by`
259Difference as patch
262#### Accepts
264`a` is a map.
266`b` is a map.
268#### Produces
270A cell of maps.
272#### Source
274```
275 +- dep
276 |* b=_a
277 ^+ [p=a q=a]
278 =+ c=~(tap by (def b))
279 =+ [d e]=[`_a`~ `_a`~]
280 |- ^+ [d e]
281 ?~ c [d e]
282 %= $
283 c t.c
284 d ?~(q.q.i.c d (~(put by d) p.i.c u.q.q.i.c))
285 e ?~(p.q.i.c e (~(put by e) p.i.c u.p.q.i.c))
286 ==
287```
289#### Examples
293---
294### `+-del:by`
296Delete
298Produces map `a` with the element located at key `b` removed.
300#### Accepts
302`a` is a map.
304`b` is a key as a noun.
306#### Produces
308A map.
310#### Source
312```
313 +- del
314 ~/ %del
315 |* b=*
316 |- ^+ a
317 ?~ a
318 ~
319 ?. =(b p.n.a)
320 ?: (gor b p.n.a)
321 [n.a $(a l.a) r.a]
322 [n.a l.a $(a r.a)]
323 |- ^- [(~ _a)]
324 ?~ l.a r.a
325 ?~ r.a l.a
326 ?: (vor p.n.l.a p.n.r.a)
327 [n.l.a l.l.a $(l.a r.l.a)]
328 [n.r.a $(r.a l.r.a) r.r.a]
329```
331#### Examples
333```
334 > =b (my [['a' 1] ['b' [2 3]] ~])
335 > (~(del by b) 'a')
336 [n=[p='b' q=[2 3]] l=~ r=~]
337```
339---
340### `+-dig:by`
342Address of key
344Produce the address of key `b` within map `a`.
346#### Accepts
348`a` is a map.
350`b` is a key as a noun.
352#### Produces
354A unit.
356#### Source
358```
359 +- dig
360 |= b=*
361 =+ c=1
362 |- ^- (unit @)
363 ?~ a ~
364 ?: =(b p.n.a) [~ u=(peg c 2)]
365 ?: (gor b p.n.a)
366 $(a l.a, c (peg c 6))
367 $(a r.a, c (peg c 7))
368```
370#### Examples
372```
373 > =b (my [['a' 1] ['b' [2 3]] ~])
374 > (~(dig by b) 'b')
375 [~ u=2]
376```
378---
379### `+-gas:by`
381Concatenate
383Insert a list of key-value pairs `b` into map `a`.
385#### Accepts
387`a` is a map.
389`b` is a list of cells of key-value nouns `p` and `q`.
391#### Produces
393A map.
395#### Source
397```
398 +- gas
399 ~/ %gas
400 |* b=(list [p=* q=*])
401 => .(b `(list _?>(?=(^ a) n.a))`b)
402 |- ^+ a
403 ?~ b
404 a
405 $(b t.b, a (put p.i.b q.i.b))
406```
408#### Examples
410```
411 > =a (my [['a' 1] ['b' 2] ~])
412 > =b [['c' 3] ['d' 4] ~]
414 > (~(gas by a) b)
415 [n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]
416```
418---
419### `+-get:by`
421Grab unit value
423Produce the unit value of the value located at key `b` within map `a`.
425#### Accepts
427`a` is a map.
429`b` is a key as a noun.
431#### Produces
433A unit.
435#### Source
437```
438 +- get
439 ~/ %get
440 |= b=*
441 ^- [$@(~ [~ u=_?>(?=(^ a) q.n.a)])]
442 =+ 42
443 ?~ a
444 ~
445 ?: =(b p.n.a)
446 [~ u=q.n.a]
447 ?: (gor b p.n.a)
448 $(a l.a)
449 $(a r.a)
450```
452#### Examples
454```
455 > =b (my [['a' 1] ['b' [2 3]] ~])
456 > (~(get by b) 'b')
457 [~ u=[2 3]]
460 > =m (my [['a' 1] ['b' 2] ~])
461 > (~(get by m) 'a')
462 [~ u=1]
463```
465---
466### `+-got:by`
468Assert
470Produce the value located at key `b` within map `a`. Crash if key `b` does not
471exist.
473#### Accepts
475`a` is a map.
477`b` is a key as a noun.
479#### Produces
481A noun.
483#### Source
485```
486 +- got
487 |* b=*
488 (need (get b))
489```
491#### Examples
493```
494 > =m (my [['a' 1] ['b' 2] ~])
495 > m
496 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
498 > (~(got by m) 'a')
499 1
501 > (~(got by m) 'c')
502 ! exit
503```
505---
506### `+-has:by`
508Key existence check
510Checks whether map `a` contains an element with key `b`, producing a flag.
512#### Accepts
514`a` is a map.
516`b` is a key as a noun.
518#### Produces
520A flag.
522#### Source
524```
525 +- has
526 ~/ %has
527 |* b=*
528 !=(~ (get b))
529```
531#### Examples
533```
534 > =b (my [['a' 1] ['b' [2 3]] ~])
535 > (~(has by b) 'b')
536 %.y
538 > (~(has by b) 'c')
539 %.n
540```
542---
543### `+-int:by`
545Intersection
547Produces a map of the (key) intersection between two maps of the same type, `a`
548and `b`. If both maps have an identical key that point to different values, the
549element from map `b` is used.
551#### Accepts
553`a` is a map.
555`b` is a map.
557#### Produces
559A map.
561#### Source
563```
564 +- int
565 ~/ %int
566 |* b=_a
567 |- ^+ a
568 ?~ b
569 ~
570 ?~ a
571 ~
572 ?: (vor p.n.a p.n.b)
573 ?: =(p.n.b p.n.a)
574 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
575 ?: (gor p.n.b p.n.a)
576 %- uni(a $(a l.a, b [n.b l.b ~])) $(b r.b)
577 %- uni(a $(a r.a, b [n.b ~ r.b])) $(b l.b)
578 ?: =(p.n.a p.n.b)
579 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
580 ?: (gor p.n.a p.n.b)
581 %- uni(a $(b l.b, a [n.a l.a ~])) $(a r.a)
582 %- uni(a $(b r.b, a [n.a ~ r.a])) $(a l.a)
583```
585#### Examples
587```
588 > =n (my [['a' 1] ['c' 3] ~])
589 > =m (my [['a' 1] ['b' 2] ~])
591 > (~(int by m) n)
592 [n=[p='a' q=1] l=~ r=~]
594 > =p (my [['a' 2] ['b' 2] ~])
595 > p
596 [n=[p='b' q=2] l={[p='a' q=2]} r={}]
597 > (~(int by p) n)
598 [n=[p='a' q=1] l=~ r=~]
600 > =q (my [['a' 2] ['c' 2] ~])
601 > q
602 [n=[p='c' q=2] l={[p='a' q=2]} r={}]
603 > (~(int by p) q)
604 [n=[p='a' q=2] l=~ r=~]
606 > =o (my [['c' 3] ['d' 4] ~])
607 > (~(int by m) o)
608 ~
609```
611---
612### `+-mar:by`
614Assert and add
616Produces map `a` with the addition of a key-value pair, where the value is a
617nonempty unit.
619Accept a noun and a unit of a noun of the type of the map's keys and values,
620respectively. Validate that the value is not null and put the pair in the map.
621If the value is null, delete the key.
623#### Accepts
625`a` is a map.
627`b` is a unit.
629#### Produces
631A map.
633#### Assert and Add
635```
636 +- mar
637 |* [b=_?>(?=(^ a) p.n.a) c=(unit ,_?>(?=(^ a) q.n.a))]
638 ?~ c
639 (del b)
640 (put b u.c)
641```
643#### Examples
645```
646 > =m (my [['a' 1] ['b' 2] ~])
647 > (~(mar by m) 'c' (some 3))
648 [n=[p='b' q=2] l=[n=[p='c' q=3] l=[n=[p='a' q=1] l={} r={}] r=~] r=~]
650 > (~(mar by m) 'c' ~)
651 [n=[p='b' q=2] l=[n=[p='a' q=1] l=~ r=~] r=~]
653 > (~(mar by m) 'b' ~)
654 [n=[p='a' q=1] l=~ r=~]
655```
657---
658### `+-put:by`
660Add key-value pair
662Produces `a` with the addition of the key-value pair of `b` and `c`.
664#### Accepts
666`a` is a map.
668`b` is a key of the same type as the keys in `a`.
670`c` is a value of the same type of the values in `a`.
672#### Produces
674A map.
676#### Source
678```
679 +- put
680 ~/ %put
681 |* [b=* c=*]
682 |- ^+ a
683 ?~ a
684 [[b c] ~ ~]
685 ?: =(b p.n.a)
686 ?: =(c q.n.a)
687 a
688 [[b c] l.a r.a]
689 ?: (gor b p.n.a)
690 =+ d=$(a l.a)
691 ?> ?=(^ d)
692 ?: (vor p.n.a p.n.d)
693 [n.a d r.a]
694 [n.d l.d [n.a r.d r.a]]
695 =+ d=$(a r.a)
696 ?> ?=(^ d)
697 ?: (vor p.n.a p.n.d)
698 [n.a l.a d]
699 [n.d [n.a l.a l.d] r.d]
700```
702#### Examples
704```
705 > =m (my [['a' 1] ['b' 2] ~])
706 > (~(put by ~) 'c' 3)
707 [n=[p='b' q=2] l=[n=[p='c' q=3] l={[p='a' q=1]} r={}] r=~]
709 > (~(put by m) "zod" 26)
710 ! nest-fail
711 ! exit
713 > (~(put by m) 'a' 2)
714 [n=[p='b' q=2] l=[n=[p='a' q=2] l={} r={}] r=~]
716 > (put:by m 'a' 2)
717 {[p=[[98 2] [[97 1] 0 0] 0] q=[97 2]]}
718```
720---
721### `+-rep:by`
723Replace by product
725Accumulate elements of map `a` using gate `b`, producing a noun.
727#### Accepts
729`a` is a map.
731`b` is a wet gate.
733#### Produces
735A noun.
737#### Source
739```
740 +- rep
741 |* b=_|=([* *] +<+)
742 |-
743 ?~ a +<+.b
744 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
745```
747#### Examples
749```
750 > =m (my [['a' 1] ['b' 2] ~])
751 > (~(rep by m) |=(a=(pair * *) p.a))
752 q=[97 1]
753```
755---
756### `+-rib:by`
758Replace values with accumulator
760#### Accepts
762`a` is a map.
764`b` is a noun.
766`c` is a wet gate.
768####
770A cell of nouns.
772#### Source
774```
775 +- rib
776 |* [b=* c=gate]
777 |- ^+ [b a]
778 ?~ a [b ~]
779 =+ d=(c n.a b)
780 =. n.a +.d
781 =+ e=$(a l.a, b -.d)
782 =+ f=$(a r.a, b -.e)
783 [-.f [n.a +.e +.f]]
784```
786#### Examples
788```
789 > =m (my [['a' 1] ['b' 2] ~])
790 > (~(rib by m) [1 2] (con by m))
792 > (~(rep by m) [1 2] |=(a=(pair * *) p.a))
793```
795---
796### `+-run:by`
798Transform values
800Iterates over every value in map `a` using gate `b`, producing a map.
802#### Accepts
804`a` is a map.
806`b` is a wet gate.
808#### Produces
810A map.
812#### Source
814```
815 +- run
816 |* b=gate
817 |-
818 ?~ a a
819 [n=[p=p.n.a q=(b q.n.a)] l=$(a l.a) r=$(a r.a)]
820```
822#### Examples
824```
825 > =m (my [['a' 1] ['b' 2] ~])
827 > ^+(m (~(run by m) dec))
828 [n=[p='b' q=1] l={[p='a' q=0]} r={}]
830 > `(map @tas @t)`(~(run by m) (cury scot %ux))
831 {[p=%a q='0x1'] [p=%b q='0x2']}
832```
834---
835### `+-rut:by`
837Transform nodes
839Applies a gate `b` to nodes in map `a`.
841#### Accepts
843`a` is a map.
845`b` is a wet gate.
847#### Produces
849A map.
851#### Source
853```
854 +- rut
855 |* b=gate
856 |-
857 ?~ a a
858 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)]
859```
861#### Examples
863```
864 > =m (my [['a' 1] ['b' 2] ~])
865```
867---
868### `+-tap:by`
870Listify pairs
872Produces the list of all elements in map `a` that is prepended to list `b`,
873which is empty by default.
875#### Accepts
877`a` is a map.
879`b` is a list.
881#### Produces
883A list.
885#### Source
887```
888 +- tap
889 =< $
890 ~/ %tap
891 =| b=(list _?>(?=(^ a) n.a))
892 |. ^+ b
893 ?~ a
894 b
895 $(a r.a, b [n.a $(a l.a)])
896```
898#### Examples
900```
901 > =m (my [['a' 1] ['b' 2] ~])
902 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
903 > `*`m
904 [[98 2] [[97 1] 0 0] 0]
906 > ~(tap by m)
907 ~[[p='b' q=2] [p='a' q=1]]
908 > `*`~(tap by m)
909 [[98 2] [97 1] 0]
910```
912---
913### `+-uni:by`
915Union
917Produces a map of the union between the keys of `a` and `b`. If `b` shares a
918key with `a`, the tuple from `b` is preserved.
920#### Accepts
922`a` is a map.
924`b` is a map.
926#### Produces
928A map.
930#### Source
932```
933 +- uni
934 ~/ %uni
935 |* b=_a
936 |- ^+ a
937 ?~ b
938 a
939 ?~ a
940 b
941 ?: (vor p.n.a p.n.b)
942 ?: =(p.n.b p.n.a)
943 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
944 ?: (gor p.n.b p.n.a)
945 $(a [n.a $(a l.a, b [n.b l.b ~]) r.a], b r.b)
946 $(a [n.a l.a $(a r.a, b [n.b ~ r.b])], b l.b)
947 ?: =(p.n.a p.n.b)
948 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
949 ?: (gor p.n.a p.n.b)
950 $(b [n.b $(b l.b, a [n.a l.a ~]) r.b], a r.a)
951 $(b [n.b l.b $(b r.b, a [n.a ~ r.a])], a l.a)
952```
954#### Examples
956```
957 > =m (my [['a' 1] ['b' 2] ~])
959 > =o (my [['c' 3] ['d' 4] ~])
961 > (~(uni by m) o)
962 [n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]
964 > (~(uni by m) ~)
965 {[p='a' q=1] [p='b' q=2]}
967 > n
968 {[p='a' q=1] [p='c' q=3]}
969 > (~(uni by o) n)
970 {[p='d' q=4] [p='a' q=1] [p='c' q=3]}
971```
973---
974### `+-urn:by`
976Turn (with key)
978Iterates over every value in map `a` using gate `b`, which accepts both
979the key and the value of each element as its sample.
981#### Accepts
983`a` is a map.
985`b` is a wet gate that accepts two nouns.
987#### Produces
989A noun.
991#### Source
993```
994 +- urn
995 |* b=$-({* *} *)
996 |-
997 ?~ a ~
998 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)
999```
1001#### Examples
1003```
1004 > m
1005 {[p='a' q=1] [p='b' q=2]}
1007 > (~(urn by m) |=(a=(pair * *) q.a))
1008 {[p='a' q=1] [p='b' q=2]}
1010 > (~(urn by m) |=(a=(pair * *) 7))
1011 {[p='a' q=7] [p='b' q=7]}
1013 > (~(urn by m) |=(a=(pair * *) p.a))
1014 {[p='a' q=97] [p='b' q=98]}
1015```
1017---
1019### `+-wyt:by`
1021Depth
1023Produce the depth of the tree map `a`.
1025#### Accepts
1027`a` is a map.
1029#### Produces
1031An atom.
1033#### Source
1035```
1036 +- wyt
1037 |- ^- @
1038 ?~(a 0 +((add $(a l.a) $(a r.a))))
1039```
1041#### Examples
1043```
1044 > =m (my [['a' 1] ['b' 2] ~])
1046 > =o (my [['c' 3] ['d' 4] ~])
1048 > ~(wyt by m)
1051 > ~(wyt by o)
1054 > ~(wyt by (~(uni by m) o))
1056```
1058---