on 979ae21 help sign in
1181 lines 17 kB history raw
1+++
2title = "2i: Map Logic"
3weight = 12
4template = "doc.html"
5+++
7### `++by`
9Map operations
11Container arm for map operation arms. A map is a set of key-value pairs. The
12contained arms inherit its sample map, `a`.
14### Accepts
16`a` is a map.
18#### Source
20```hoon
21 ++ by
22 ~/ %by
23 =| a=(tree (pair))
24 =* node ?>(?=(^ a) n.a)
25```
27#### Examples
29```
30 > ~(. by (my [%a 1] [%b 2] ~))
31 < 27.zjr
32 { a
33 ?(
34 { n/{?(p/$a p/$b) q/@ud}
35 l/nlr({p/?($b $a) q/@ud})
36 r/nlr({p/?($b $a) q/@ud})
37 }
38 $~
39 )
40 <119.wim 31.ohr 1.jmk $143>
41 }
42```
44---
45### `+-all:by`
47Logical AND
49Computes the logical AND on the results of slamming every element in map `a`
50with gate `b`.
52#### Accepts
54`a` is a map.
56`b` is a wet gate.
58#### Produces
60A flag.
62#### Source
64```hoon
65 +- all
66 ~/ %all
67 |* b=$-(* ?)
68 |- ^- ?
69 ?~ a
70 &
71 ?&((b q.n.a) $(a l.a) $(a r.a))
72```
74#### Examples
76```
77 > =b (my [['a' 1] ['b' [2 3]] ~])
78 > (~(all by b) |=(a=* ?@(a & |)))
79 %.n
81 > =a (my [['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
82 > (~(all by a) |=(a=@ (lte a 6)))
83 %.y
85 > (~(all by a) |=(a=@ (lte a 4)))
86 %.n
87```
89---
90### `+-any:by`
92Logical OR
94Computes the logical OR on the results of slamming every element with
95gate `b`.
97#### Accepts
99`a` is a map.
101`b` is a wet gate.
103#### Produces
105A flag.
107#### Source
109```hoon
110 +- any
111 ~/ %any
112 |* b=$-(* ?)
113 |- ^- ?
114 ?~ a
115 |
116 ?|((b q.n.a) $(a l.a) $(a r.a))
117```
119#### Examples
121```
122 > =b (my [['a' 1] ['b' [2 3]] ~])
123 > (~(any by b) |=(a=* ?@(a & |)))
124 %.y
126 > =a (my [['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
127 > (~(any by a) |=(a=@ (lte a 4)))
128 %.y
129```
131---
132### `+-apt:by`
134Check correctness
136Computes whether `a` has a correct horizontal order and a correct vertical
137order, producing a flag.
139#### Accepts
141`a` is a map.
143#### Produces
145A flag.
147#### Source
149```hoon
150 +- apt
151 =| [l=(unit) r=(unit)]
152 |- ^- ?
153 ?~ a &
154 ?& ?~(l & (gor p.n.a u.l))
155 ?~(r & (gor u.r p.n.a))
156 ?~(l.a & ?&((vor p.n.a p.n.l.a) $(a l.a, l `p.n.a)))
157 ?~(r.a & ?&((vor p.n.a p.n.r.a) $(a r.a, r `p.n.a)))
158 ==
159```
161#### Examples
163```
164 > =a (my [['a' 1] ['b' [2 3]] ~])
165 > ~(apt by a)
166 %.y
168 > =s (my [[1 2] [3 4] ~])
169 > =z ?~ s ~ s(p.n 10)
170 > z
171 [n=[p=10 q=2] l={} r={[p=3 q=4]}]
173 > ~(apt by z)
174 %.n
175```
177#### Discussion
179See section [`2f`](@/docs/hoon/reference/stdlib/2f.md) for more information on noun ordering.
181### `+-bif:by`
183Bifurcate
185Splits map `a` into two maps `l` and `r`, with the head at `b` discarded.
187#### Accepts
189`a` is a map.
191`b` is a noun.
193`c` is a noun.
195#### Produces
197A cell of two maps.
199#### Source
201```hoon
202 +- bif
203 ~/ %bif
204 |* [b=* c=*]
205 ^+ [l=a r=a]
206 =< +
207 |- ^+ a
208 ?~ a
209 [[b c] ~ ~]
210 ?: =(b p.n.a)
211 ?: =(c q.n.a)
212 a
213 [[b c] l.a r.a]
214 ?: (gor b p.n.a)
215 =+ d=$(a l.a)
216 ?> ?=(^ d)
217 [n.d l.d [n.a r.d r.a]]
218 =+ d=$(a r.a)
219 ?> ?=(^ d)
220 [n.d [n.a l.a l.d] r.d]
221```
223#### Examples
225```
226 > =a (my [['a' 1] ['b' 2] ['c' 3] ~])
228 > (~(bif by a) ['b' 2])
229 [l=[n=[p='c' q=3] l=[n=[p='a' q=1] l={} r={}] r=~] r=~]
230```
232### `+-def:by`
234Difference
236#### Accepts
238`a` is a map.
240`b` is a map.
242#### Produces
244A map.
246#### Source
248```hoon
249 +- def
250 |* b=_a
251 ^- (map _p:node (pair (unit _q:node) (unit _q:node)))
252 !!
253```
255#### Examples
257---
258### `+-dep:by`
260Difference as patch
263#### Accepts
265`a` is a map.
267`b` is a map.
269#### Produces
271A cell of maps.
273#### Source
275```hoon
276 +- dep
277 |* b=_a
278 ^+ [p=a q=a]
279 =+ c=~(tap by (def b))
280 =+ [d e]=[`_a`~ `_a`~]
281 |- ^+ [d e]
282 ?~ c [d e]
283 %= $
284 c t.c
285 d ?~(q.q.i.c d (~(put by d) p.i.c u.q.q.i.c))
286 e ?~(p.q.i.c e (~(put by e) p.i.c u.p.q.i.c))
287 ==
288```
290#### Examples
294---
295### `+-del:by`
297Delete
299Produces map `a` with the element located at key `b` removed.
301#### Accepts
303`a` is a map.
305`b` is a key as a noun.
307#### Produces
309A map.
311#### Source
313```hoon
314 +- del
315 ~/ %del
316 |* b=*
317 |- ^+ a
318 ?~ a
319 ~
320 ?. =(b p.n.a)
321 ?: (gor b p.n.a)
322 [n.a $(a l.a) r.a]
323 [n.a l.a $(a r.a)]
324 |- ^- [(~ _a)]
325 ?~ l.a r.a
326 ?~ r.a l.a
327 ?: (vor p.n.l.a p.n.r.a)
328 [n.l.a l.l.a $(l.a r.l.a)]
329 [n.r.a $(r.a l.r.a) r.r.a]
330```
332#### Examples
334```
335 > =b (my [['a' 1] ['b' [2 3]] ~])
336 > (~(del by b) 'a')
337 [n=[p='b' q=[2 3]] l=~ r=~]
338```
340---
341### `+-dig:by`
343Address of key
345Produce the address of key `b` within map `a`.
347#### Accepts
349`a` is a map.
351`b` is a key as a noun.
353#### Produces
355A unit.
357#### Source
359```hoon
360 +- dig
361 |= b=*
362 =+ c=1
363 |- ^- (unit @)
364 ?~ a ~
365 ?: =(b p.n.a) [~ u=(peg c 2)]
366 ?: (gor b p.n.a)
367 $(a l.a, c (peg c 6))
368 $(a r.a, c (peg c 7))
369```
371#### Examples
373```
374 > =b (my [['a' 1] ['b' [2 3]] ~])
375 > (~(dig by b) 'b')
376 [~ u=2]
377```
379---
380### `+-gas:by`
382Concatenate
384Insert a list of key-value pairs `b` into map `a`.
386#### Accepts
388`a` is a map.
390`b` is a list of cells of key-value nouns `p` and `q`.
392#### Produces
394A map.
396#### Source
398```hoon
399 +- gas
400 ~/ %gas
401 |* b=(list [p=* q=*])
402 => .(b `(list _?>(?=(^ a) n.a))`b)
403 |- ^+ a
404 ?~ b
405 a
406 $(b t.b, a (put p.i.b q.i.b))
407```
409#### Examples
411```
412 > =a (my [['a' 1] ['b' 2] ~])
413 > =b [['c' 3] ['d' 4] ~]
415 > (~(gas by a) b)
416 [n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]
417```
419---
420### `+-get:by`
422Grab unit value
424Produce the unit value of the value located at key `b` within map `a`.
426#### Accepts
428`a` is a map.
430`b` is a key as a noun.
432#### Produces
434A unit.
436#### Source
438```hoon
439 +- get
440 ~/ %get
441 |= b=*
442 ^- [$@(~ [~ u=_?>(?=(^ a) q.n.a)])]
443 =+ 42
444 ?~ a
445 ~
446 ?: =(b p.n.a)
447 [~ u=q.n.a]
448 ?: (gor b p.n.a)
449 $(a l.a)
450 $(a r.a)
451```
453#### Examples
455```
456 > =b (my [['a' 1] ['b' [2 3]] ~])
457 > (~(get by b) 'b')
458 [~ u=[2 3]]
461 > =m (my [['a' 1] ['b' 2] ~])
462 > (~(get by m) 'a')
463 [~ u=1]
464```
466---
467### `+-got:by`
469Assert
471Produce the value located at key `b` within map `a`. Crash if key `b` does not
472exist.
474#### Accepts
476`a` is a map.
478`b` is a key as a noun.
480#### Produces
482A noun.
484#### Source
486```hoon
487 +- got
488 |* b=*
489 (need (get b))
490```
492#### Examples
494```
495 > =m (my [['a' 1] ['b' 2] ~])
496 > m
497 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
499 > (~(got by m) 'a')
500 1
502 > (~(got by m) 'c')
503 ! exit
504```
506---
507### `+-gut:by`
509Grab value with default
511Produce the value located at key `b` within map `a`. Use default value `c` if
512key does not exist.
514#### Accepts
516`a` is a map.
518`b` is a key as a noun.
520`c` is a noun.
522#### Produces
524A noun.
526#### Source
528```hoon
529 ++ gut :: fall value by key
530 |* [b=* c=*]
531 (fall (get b) c)
533```
535#### Examples
537```
538 > =m (my [['a' 1] ['b' 2] ~])
539 > m
540 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
542 > (~(gut by m) 'a' 0)
543 1
545 > (~(gut by m) 'c' 0)
546 0
547```
549---
550### `+-has:by`
552Key existence check
554Checks whether map `a` contains an element with key `b`, producing a flag.
556#### Accepts
558`a` is a map.
560`b` is a key as a noun.
562#### Produces
564A flag.
566#### Source
568```hoon
569 +- has
570 ~/ %has
571 |* b=*
572 !=(~ (get b))
573```
575#### Examples
577```
578 > =b (my [['a' 1] ['b' [2 3]] ~])
579 > (~(has by b) 'b')
580 %.y
582 > (~(has by b) 'c')
583 %.n
584```
586---
587### `+-int:by`
589Intersection
591Produces a map of the (key) intersection between two maps of the same type, `a`
592and `b`. If both maps have an identical key that point to different values, the
593element from map `b` is used.
595#### Accepts
597`a` is a map.
599`b` is a map.
601#### Produces
603A map.
605#### Source
607```hoon
608 +- int
609 ~/ %int
610 |* b=_a
611 |- ^+ a
612 ?~ b
613 ~
614 ?~ a
615 ~
616 ?: (vor p.n.a p.n.b)
617 ?: =(p.n.b p.n.a)
618 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
619 ?: (gor p.n.b p.n.a)
620 %- uni(a $(a l.a, b [n.b l.b ~])) $(b r.b)
621 %- uni(a $(a r.a, b [n.b ~ r.b])) $(b l.b)
622 ?: =(p.n.a p.n.b)
623 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
624 ?: (gor p.n.a p.n.b)
625 %- uni(a $(b l.b, a [n.a l.a ~])) $(a r.a)
626 %- uni(a $(b r.b, a [n.a ~ r.a])) $(a l.a)
627```
629#### Examples
631```
632 > =n (my [['a' 1] ['c' 3] ~])
633 > =m (my [['a' 1] ['b' 2] ~])
635 > (~(int by m) n)
636 [n=[p='a' q=1] l=~ r=~]
638 > =p (my [['a' 2] ['b' 2] ~])
639 > p
640 [n=[p='b' q=2] l={[p='a' q=2]} r={}]
641 > (~(int by p) n)
642 [n=[p='a' q=1] l=~ r=~]
644 > =q (my [['a' 2] ['c' 2] ~])
645 > q
646 [n=[p='c' q=2] l={[p='a' q=2]} r={}]
647 > (~(int by p) q)
648 [n=[p='a' q=2] l=~ r=~]
650 > =o (my [['c' 3] ['d' 4] ~])
651 > (~(int by m) o)
652 ~
653```
655---
657### `++ key:by`
659Set of keys
661Produces a set of all keys in map `a` that is prepended to set `b`,
662which is empty by default.
664#### Accepts
666`a` is a map.
668`b` is a set.
670#### Produces
672A set.
674#### Source
676```hoon
677++ key
678 =+ b=`(set _?>(?=(^ a) p.n.a))`~
679 |- ^+ b
680 ?~ a b
681 $(a r.a, b $(a l.a, b (~(put in b) p.n.a)))
682```
684#### Examples
686```
687 > =m (my [['a' 1] ['b' 2] ~])
688 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
690 > ~(key by m)
691 {'b' 'a'}
692```
694---
696### `+-mar:by`
698Assert and add
700Produces map `a` with the addition of a key-value pair, where the value is a
701nonempty unit.
703Accept a noun and a unit of a noun of the type of the map's keys and values,
704respectively. Validate that the value is not null and put the pair in the map.
705If the value is null, delete the key.
707#### Accepts
709`a` is a map.
711`b` is a unit.
713#### Produces
715A map.
717#### Assert and Add
719```hoon
720 +- mar
721 |* [b=_?>(?=(^ a) p.n.a) c=(unit ,_?>(?=(^ a) q.n.a))]
722 ?~ c
723 (del b)
724 (put b u.c)
725```
727#### Examples
729```
730 > =m (my [['a' 1] ['b' 2] ~])
731 > (~(mar by m) 'c' (some 3))
732 [n=[p='b' q=2] l=[n=[p='c' q=3] l=[n=[p='a' q=1] l={} r={}] r=~] r=~]
734 > (~(mar by m) 'c' ~)
735 [n=[p='b' q=2] l=[n=[p='a' q=1] l=~ r=~] r=~]
737 > (~(mar by m) 'b' ~)
738 [n=[p='a' q=1] l=~ r=~]
739```
741---
742### `+-put:by`
744Add key-value pair
746Produces `a` with the addition of the key-value pair of `b` and `c`.
748#### Accepts
750`a` is a map.
752`b` is a key of the same type as the keys in `a`.
754`c` is a value of the same type of the values in `a`.
756#### Produces
758A map.
760#### Source
762```hoon
763 +- put
764 ~/ %put
765 |* [b=* c=*]
766 |- ^+ a
767 ?~ a
768 [[b c] ~ ~]
769 ?: =(b p.n.a)
770 ?: =(c q.n.a)
771 a
772 [[b c] l.a r.a]
773 ?: (gor b p.n.a)
774 =+ d=$(a l.a)
775 ?> ?=(^ d)
776 ?: (vor p.n.a p.n.d)
777 [n.a d r.a]
778 [n.d l.d [n.a r.d r.a]]
779 =+ d=$(a r.a)
780 ?> ?=(^ d)
781 ?: (vor p.n.a p.n.d)
782 [n.a l.a d]
783 [n.d [n.a l.a l.d] r.d]
784```
786#### Examples
788```
789 > =m (my [['a' 1] ['b' 2] ~])
790 > (~(put by ~) 'c' 3)
791 [n=[p='b' q=2] l=[n=[p='c' q=3] l={[p='a' q=1]} r={}] r=~]
793 > (~(put by m) "zod" 26)
794 ! nest-fail
795 ! exit
797 > (~(put by m) 'a' 2)
798 [n=[p='b' q=2] l=[n=[p='a' q=2] l={} r={}] r=~]
800 > (put:by m 'a' 2)
801 {[p=[[98 2] [[97 1] 0 0] 0] q=[97 2]]}
802```
804---
805### `+-rep:by`
807Replace by product
809Accumulate elements of map `a` using gate `b`, producing a noun.
811#### Accepts
813`a` is a map.
815`b` is a wet gate.
817#### Produces
819A noun.
821#### Source
823```hoon
824 +- rep
825 |* b=_|=([* *] +<+)
826 |-
827 ?~ a +<+.b
828 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
829```
831#### Examples
833```
834 > =m (my [['a' 1] ['b' 2] ~])
835 > (~(rep by m) |=(a=(pair * *) p.a))
836 q=[97 1]
837```
839---
840### `+-rib:by`
842Replace values with accumulator
844#### Accepts
846`a` is a map.
848`b` is a noun.
850`c` is a wet gate.
852####
854A cell of nouns.
856#### Source
858```hoon
859 +- rib
860 |* [b=* c=gate]
861 |- ^+ [b a]
862 ?~ a [b ~]
863 =+ d=(c n.a b)
864 =. n.a +.d
865 =+ e=$(a l.a, b -.d)
866 =+ f=$(a r.a, b -.e)
867 [-.f [n.a +.e +.f]]
868```
870#### Examples
872```
873 > =m (my [['a' 1] ['b' 2] ~])
874 > (~(rib by m) [1 2] (con by m))
876 > (~(rep by m) [1 2] |=(a=(pair * *) p.a))
877```
879---
880### `+-run:by`
882Transform values
884Iterates over every value in map `a` using gate `b`, producing a map.
886#### Accepts
888`a` is a map.
890`b` is a wet gate.
892#### Produces
894A map.
896#### Source
898```hoon
899 +- run
900 |* b=gate
901 |-
902 ?~ a a
903 [n=[p=p.n.a q=(b q.n.a)] l=$(a l.a) r=$(a r.a)]
904```
906#### Examples
908```
909 > =m (my [['a' 1] ['b' 2] ~])
911 > ^+(m (~(run by m) dec))
912 [n=[p='b' q=1] l={[p='a' q=0]} r={}]
914 > `(map @tas @t)`(~(run by m) (cury scot %ux))
915 {[p=%a q='0x1'] [p=%b q='0x2']}
916```
918---
919### `+-rut:by`
921Transform nodes
923Applies a gate `b` to nodes in map `a`.
925#### Accepts
927`a` is a map.
929`b` is a wet gate.
931#### Produces
933A map.
935#### Source
937```hoon
938 +- rut
939 |* b=gate
940 |-
941 ?~ a a
942 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)]
943```
945#### Examples
947```
948 > =m (my [['a' 1] ['b' 2] ~])
949```
951---
952### `+-tap:by`
954Listify pairs
956Produces the list of all elements in map `a` that is prepended to list `b`,
957which is empty by default.
959#### Accepts
961`a` is a map.
963`b` is a list.
965#### Produces
967A list.
969#### Source
971```hoon
972 +- tap
973 =< $
974 ~/ %tap
975 =| b=(list _?>(?=(^ a) n.a))
976 |. ^+ b
977 ?~ a
978 b
979 $(a r.a, b [n.a $(a l.a)])
980```
982#### Examples
984```
985 > =m (my [['a' 1] ['b' 2] ~])
986 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
987 > `*`m
988 [[98 2] [[97 1] 0 0] 0]
990 > ~(tap by m)
991 ~[[p='b' q=2] [p='a' q=1]]
992 > `*`~(tap by m)
993 [[98 2] [97 1] 0]
994```
996---
997### `+-uni:by`
999Union
1001Produces a map of the union between the keys of `a` and `b`. If `b` shares a
1002key with `a`, the tuple from `b` is preserved.
1004#### Accepts
1006`a` is a map.
1008`b` is a map.
1010#### Produces
1012A map.
1014#### Source
1016```hoon
1017 +- uni
1018 ~/ %uni
1019 |* b=_a
1020 |- ^+ a
1021 ?~ b
1023 ?~ a
1025 ?: (vor p.n.a p.n.b)
1026 ?: =(p.n.b p.n.a)
1027 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
1028 ?: (gor p.n.b p.n.a)
1029 $(a [n.a $(a l.a, b [n.b l.b ~]) r.a], b r.b)
1030 $(a [n.a l.a $(a r.a, b [n.b ~ r.b])], b l.b)
1031 ?: =(p.n.a p.n.b)
1032 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
1033 ?: (gor p.n.a p.n.b)
1034 $(b [n.b $(b l.b, a [n.a l.a ~]) r.b], a r.a)
1035 $(b [n.b l.b $(b r.b, a [n.a ~ r.a])], a l.a)
1036```
1038#### Examples
1040```
1041 > =m (my [['a' 1] ['b' 2] ~])
1043 > =o (my [['c' 3] ['d' 4] ~])
1045 > (~(uni by m) o)
1046 [n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]
1048 > (~(uni by m) ~)
1049 {[p='a' q=1] [p='b' q=2]}
1051 > n
1052 {[p='a' q=1] [p='c' q=3]}
1053 > (~(uni by o) n)
1054 {[p='d' q=4] [p='a' q=1] [p='c' q=3]}
1055```
1057---
1058### `+-urn:by`
1060Turn (with key)
1062Iterates over every value in map `a` using gate `b`, which accepts both
1063the key and the value of each element as its sample.
1065#### Accepts
1067`a` is a map.
1069`b` is a wet gate that accepts two nouns.
1071#### Produces
1073A noun.
1075#### Source
1077```hoon
1078 +- urn
1079 |* b=$-({* *} *)
1080 |-
1081 ?~ a ~
1082 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)
1083```
1085#### Examples
1087```
1088 > m
1089 {[p='a' q=1] [p='b' q=2]}
1091 > (~(urn by m) |=(a=(pair * *) q.a))
1092 {[p='a' q=1] [p='b' q=2]}
1094 > (~(urn by m) |=(a=(pair * *) 7))
1095 {[p='a' q=7] [p='b' q=7]}
1097 > (~(urn by m) |=(a=(pair * *) p.a))
1098 {[p='a' q=97] [p='b' q=98]}
1099```
1101---
1103### `+-wyt:by`
1105Depth
1107Produce the depth of the tree map `a`.
1109#### Accepts
1111`a` is a map.
1113#### Produces
1115An atom.
1117#### Source
1119```hoon
1120 +- wyt
1121 |- ^- @
1122 ?~(a 0 +((add $(a l.a) $(a r.a))))
1123```
1125#### Examples
1127```
1128 > =m (my [['a' 1] ['b' 2] ~])
1130 > =o (my [['c' 3] ['d' 4] ~])
1132 > ~(wyt by m)
1135 > ~(wyt by o)
1138 > ~(wyt by (~(uni by m) o))
1140```
1142---
1144### `++ val:by`
1146List of values
1148Produces a list of all values in map `a` that is prepended to list `b`,
1149which is empty by default.
1151#### Accepts
1153`a` is a map.
1155`b` is a list.
1157#### Produces
1159A list.
1161#### Source
1163```hoon
1164++ val
1165 =+ b=`(list _?>(?=(^ a) q.n.a))`~
1166 |- ^+ b
1167 ?~ a b
1168 $(a r.a, b [q.n.a $(a l.a)])
1169```
1171#### Examples
1173```
1174 > =m (my [['a' 1] ['b' 2] ~])
1175 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
1177 > ~(val by m)
1178 ~[1 2]
1179```
1181---