on 72a5ca5 help sign in
1155 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### `+-dep:by`
234Difference as patch
237#### Accepts
239`a` is a map.
241`b` is a map.
243#### Produces
245A cell of maps.
247#### Source
249```hoon
250 +- dep
251 |* b=_a
252 ^+ [p=a q=a]
253 =+ c=~(tap by (def b))
254 =+ [d e]=[`_a`~ `_a`~]
255 |- ^+ [d e]
256 ?~ c [d e]
257 %= $
258 c t.c
259 d ?~(q.q.i.c d (~(put by d) p.i.c u.q.q.i.c))
260 e ?~(p.q.i.c e (~(put by e) p.i.c u.p.q.i.c))
261 ==
262```
264#### Examples
268---
269### `+-del:by`
271Delete
273Produces map `a` with the element located at key `b` removed.
275#### Accepts
277`a` is a map.
279`b` is a key as a noun.
281#### Produces
283A map.
285#### Source
287```hoon
288 +- del
289 ~/ %del
290 |* b=*
291 |- ^+ a
292 ?~ a
293 ~
294 ?. =(b p.n.a)
295 ?: (gor b p.n.a)
296 [n.a $(a l.a) r.a]
297 [n.a l.a $(a r.a)]
298 |- ^- [(~ _a)]
299 ?~ l.a r.a
300 ?~ r.a l.a
301 ?: (vor p.n.l.a p.n.r.a)
302 [n.l.a l.l.a $(l.a r.l.a)]
303 [n.r.a $(r.a l.r.a) r.r.a]
304```
306#### Examples
308```
309 > =b (my [['a' 1] ['b' [2 3]] ~])
310 > (~(del by b) 'a')
311 [n=[p='b' q=[2 3]] l=~ r=~]
312```
314---
315### `+-dig:by`
317Address of key
319Produce the address of key `b` within map `a`.
321#### Accepts
323`a` is a map.
325`b` is a key as a noun.
327#### Produces
329A unit.
331#### Source
333```hoon
334 +- dig
335 |= b=*
336 =+ c=1
337 |- ^- (unit @)
338 ?~ a ~
339 ?: =(b p.n.a) [~ u=(peg c 2)]
340 ?: (gor b p.n.a)
341 $(a l.a, c (peg c 6))
342 $(a r.a, c (peg c 7))
343```
345#### Examples
347```
348 > =b (my [['a' 1] ['b' [2 3]] ~])
349 > (~(dig by b) 'b')
350 [~ u=2]
351```
353---
354### `+-gas:by`
356Concatenate
358Insert a list of key-value pairs `b` into map `a`.
360#### Accepts
362`a` is a map.
364`b` is a list of cells of key-value nouns `p` and `q`.
366#### Produces
368A map.
370#### Source
372```hoon
373 +- gas
374 ~/ %gas
375 |* b=(list [p=* q=*])
376 => .(b `(list _?>(?=(^ a) n.a))`b)
377 |- ^+ a
378 ?~ b
379 a
380 $(b t.b, a (put p.i.b q.i.b))
381```
383#### Examples
385```
386 > =a (my [['a' 1] ['b' 2] ~])
387 > =b [['c' 3] ['d' 4] ~]
389 > (~(gas by a) b)
390 [n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]
391```
393---
394### `+-get:by`
396Grab unit value
398Produce the unit value of the value located at key `b` within map `a`.
400#### Accepts
402`a` is a map.
404`b` is a key as a noun.
406#### Produces
408A unit.
410#### Source
412```hoon
413 +- get
414 ~/ %get
415 |= b=*
416 ^- [$@(~ [~ u=_?>(?=(^ a) q.n.a)])]
417 =+ 42
418 ?~ a
419 ~
420 ?: =(b p.n.a)
421 [~ u=q.n.a]
422 ?: (gor b p.n.a)
423 $(a l.a)
424 $(a r.a)
425```
427#### Examples
429```
430 > =b (my [['a' 1] ['b' [2 3]] ~])
431 > (~(get by b) 'b')
432 [~ u=[2 3]]
435 > =m (my [['a' 1] ['b' 2] ~])
436 > (~(get by m) 'a')
437 [~ u=1]
438```
440---
441### `+-got:by`
443Assert
445Produce the value located at key `b` within map `a`. Crash if key `b` does not
446exist.
448#### Accepts
450`a` is a map.
452`b` is a key as a noun.
454#### Produces
456A noun.
458#### Source
460```hoon
461 +- got
462 |* b=*
463 (need (get b))
464```
466#### Examples
468```
469 > =m (my [['a' 1] ['b' 2] ~])
470 > m
471 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
473 > (~(got by m) 'a')
474 1
476 > (~(got by m) 'c')
477 ! exit
478```
480---
481### `+-gut:by`
483Grab value with default
485Produce the value located at key `b` within map `a`. Use default value `c` if
486key does not exist.
488#### Accepts
490`a` is a map.
492`b` is a key as a noun.
494`c` is a noun.
496#### Produces
498A noun.
500#### Source
502```hoon
503 ++ gut :: fall value by key
504 |* [b=* c=*]
505 (fall (get b) c)
507```
509#### Examples
511```
512 > =m (my [['a' 1] ['b' 2] ~])
513 > m
514 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
516 > (~(gut by m) 'a' 0)
517 1
519 > (~(gut by m) 'c' 0)
520 0
521```
523---
524### `+-has:by`
526Key existence check
528Checks whether map `a` contains an element with key `b`, producing a flag.
530#### Accepts
532`a` is a map.
534`b` is a key as a noun.
536#### Produces
538A flag.
540#### Source
542```hoon
543 +- has
544 ~/ %has
545 |* b=*
546 !=(~ (get b))
547```
549#### Examples
551```
552 > =b (my [['a' 1] ['b' [2 3]] ~])
553 > (~(has by b) 'b')
554 %.y
556 > (~(has by b) 'c')
557 %.n
558```
560---
561### `+-int:by`
563Intersection
565Produces a map of the (key) intersection between two maps of the same type, `a`
566and `b`. If both maps have an identical key that point to different values, the
567element from map `b` is used.
569#### Accepts
571`a` is a map.
573`b` is a map.
575#### Produces
577A map.
579#### Source
581```hoon
582 +- int
583 ~/ %int
584 |* b=_a
585 |- ^+ a
586 ?~ b
587 ~
588 ?~ a
589 ~
590 ?: (vor p.n.a p.n.b)
591 ?: =(p.n.b p.n.a)
592 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
593 ?: (gor p.n.b p.n.a)
594 %- uni(a $(a l.a, b [n.b l.b ~])) $(b r.b)
595 %- uni(a $(a r.a, b [n.b ~ r.b])) $(b l.b)
596 ?: =(p.n.a p.n.b)
597 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
598 ?: (gor p.n.a p.n.b)
599 %- uni(a $(b l.b, a [n.a l.a ~])) $(a r.a)
600 %- uni(a $(b r.b, a [n.a ~ r.a])) $(a l.a)
601```
603#### Examples
605```
606 > =n (my [['a' 1] ['c' 3] ~])
607 > =m (my [['a' 1] ['b' 2] ~])
609 > (~(int by m) n)
610 [n=[p='a' q=1] l=~ r=~]
612 > =p (my [['a' 2] ['b' 2] ~])
613 > p
614 [n=[p='b' q=2] l={[p='a' q=2]} r={}]
615 > (~(int by p) n)
616 [n=[p='a' q=1] l=~ r=~]
618 > =q (my [['a' 2] ['c' 2] ~])
619 > q
620 [n=[p='c' q=2] l={[p='a' q=2]} r={}]
621 > (~(int by p) q)
622 [n=[p='a' q=2] l=~ r=~]
624 > =o (my [['c' 3] ['d' 4] ~])
625 > (~(int by m) o)
626 ~
627```
629---
631### `++ key:by`
633Set of keys
635Produces a set of all keys in map `a` that is prepended to set `b`,
636which is empty by default.
638#### Accepts
640`a` is a map.
642`b` is a set.
644#### Produces
646A set.
648#### Source
650```hoon
651++ key
652 =+ b=`(set _?>(?=(^ a) p.n.a))`~
653 |- ^+ b
654 ?~ a b
655 $(a r.a, b $(a l.a, b (~(put in b) p.n.a)))
656```
658#### Examples
660```
661 > =m (my [['a' 1] ['b' 2] ~])
662 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
664 > ~(key by m)
665 {'b' 'a'}
666```
668---
670### `+-mar:by`
672Assert and add
674Produces map `a` with the addition of a key-value pair, where the value is a
675nonempty unit.
677Accept a noun and a unit of a noun of the type of the map's keys and values,
678respectively. Validate that the value is not null and put the pair in the map.
679If the value is null, delete the key.
681#### Accepts
683`a` is a map.
685`b` is a unit.
687#### Produces
689A map.
691#### Assert and Add
693```hoon
694 +- mar
695 |* [b=_?>(?=(^ a) p.n.a) c=(unit ,_?>(?=(^ a) q.n.a))]
696 ?~ c
697 (del b)
698 (put b u.c)
699```
701#### Examples
703```
704 > =m (my [['a' 1] ['b' 2] ~])
705 > (~(mar by m) 'c' (some 3))
706 [n=[p='b' q=2] l=[n=[p='c' q=3] l=[n=[p='a' q=1] l={} r={}] r=~] r=~]
708 > (~(mar by m) 'c' ~)
709 [n=[p='b' q=2] l=[n=[p='a' q=1] l=~ r=~] r=~]
711 > (~(mar by m) 'b' ~)
712 [n=[p='a' q=1] l=~ r=~]
713```
715---
716### `+-put:by`
718Add key-value pair
720Produces `a` with the addition of the key-value pair of `b` and `c`.
722#### Accepts
724`a` is a map.
726`b` is a key of the same type as the keys in `a`.
728`c` is a value of the same type of the values in `a`.
730#### Produces
732A map.
734#### Source
736```hoon
737 +- put
738 ~/ %put
739 |* [b=* c=*]
740 |- ^+ a
741 ?~ a
742 [[b c] ~ ~]
743 ?: =(b p.n.a)
744 ?: =(c q.n.a)
745 a
746 [[b c] l.a r.a]
747 ?: (gor b p.n.a)
748 =+ d=$(a l.a)
749 ?> ?=(^ d)
750 ?: (vor p.n.a p.n.d)
751 [n.a d r.a]
752 [n.d l.d [n.a r.d r.a]]
753 =+ d=$(a r.a)
754 ?> ?=(^ d)
755 ?: (vor p.n.a p.n.d)
756 [n.a l.a d]
757 [n.d [n.a l.a l.d] r.d]
758```
760#### Examples
762```
763 > =m (my [['a' 1] ['b' 2] ~])
764 > (~(put by ~) 'c' 3)
765 [n=[p='b' q=2] l=[n=[p='c' q=3] l={[p='a' q=1]} r={}] r=~]
767 > (~(put by m) "zod" 26)
768 ! nest-fail
769 ! exit
771 > (~(put by m) 'a' 2)
772 [n=[p='b' q=2] l=[n=[p='a' q=2] l={} r={}] r=~]
774 > (put:by m 'a' 2)
775 {[p=[[98 2] [[97 1] 0 0] 0] q=[97 2]]}
776```
778---
779### `+-rep:by`
781Replace by product
783Accumulate elements of map `a` using gate `b`, producing a noun.
785#### Accepts
787`a` is a map.
789`b` is a wet gate.
791#### Produces
793A noun.
795#### Source
797```hoon
798 +- rep
799 |* b=_|=([* *] +<+)
800 |-
801 ?~ a +<+.b
802 $(a r.a, +<+.b $(a l.a, +<+.b (b n.a +<+.b)))
803```
805#### Examples
807```
808 > =m (my [['a' 1] ['b' 2] ~])
809 > (~(rep by m) |=(a=(pair * *) p.a))
810 q=[97 1]
811```
813---
814### `+-rib:by`
816Replace values with accumulator
818#### Accepts
820`a` is a map.
822`b` is a noun.
824`c` is a wet gate.
826####
828A cell of nouns.
830#### Source
832```hoon
833 +- rib
834 |* [b=* c=gate]
835 |- ^+ [b a]
836 ?~ a [b ~]
837 =+ d=(c n.a b)
838 =. n.a +.d
839 =+ e=$(a l.a, b -.d)
840 =+ f=$(a r.a, b -.e)
841 [-.f [n.a +.e +.f]]
842```
844#### Examples
846```
847 > =m (my [['a' 1] ['b' 2] ~])
848 > (~(rib by m) [1 2] (con by m))
850 > (~(rep by m) [1 2] |=(a=(pair * *) p.a))
851```
853---
854### `+-run:by`
856Transform values
858Iterates over every value in map `a` using gate `b`, producing a map.
860#### Accepts
862`a` is a map.
864`b` is a wet gate.
866#### Produces
868A map.
870#### Source
872```hoon
873 +- run
874 |* b=gate
875 |-
876 ?~ a a
877 [n=[p=p.n.a q=(b q.n.a)] l=$(a l.a) r=$(a r.a)]
878```
880#### Examples
882```
883 > =m (my [['a' 1] ['b' 2] ~])
885 > ^+(m (~(run by m) dec))
886 [n=[p='b' q=1] l={[p='a' q=0]} r={}]
888 > `(map @tas @t)`(~(run by m) (cury scot %ux))
889 {[p=%a q='0x1'] [p=%b q='0x2']}
890```
892---
893### `+-rut:by`
895Transform nodes
897Applies a gate `b` to nodes in map `a`.
899#### Accepts
901`a` is a map.
903`b` is a wet gate.
905#### Produces
907A map.
909#### Source
911```hoon
912 +- rut
913 |* b=gate
914 |-
915 ?~ a a
916 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)]
917```
919#### Examples
921```
922 > =m (my [['a' 1] ['b' 2] ~])
923```
925---
926### `+-tap:by`
928Listify pairs
930Produces the list of all elements in map `a` that is prepended to list `b`,
931which is empty by default.
933#### Accepts
935`a` is a map.
937`b` is a list.
939#### Produces
941A list.
943#### Source
945```hoon
946 +- tap
947 =< $
948 ~/ %tap
949 =| b=(list _?>(?=(^ a) n.a))
950 |. ^+ b
951 ?~ a
952 b
953 $(a r.a, b [n.a $(a l.a)])
954```
956#### Examples
958```
959 > =m (my [['a' 1] ['b' 2] ~])
960 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
961 > `*`m
962 [[98 2] [[97 1] 0 0] 0]
964 > ~(tap by m)
965 ~[[p='b' q=2] [p='a' q=1]]
966 > `*`~(tap by m)
967 [[98 2] [97 1] 0]
968```
970---
971### `+-uni:by`
973Union
975Produces a map of the union between the keys of `a` and `b`. If `b` shares a
976key with `a`, the tuple from `b` is preserved.
978#### Accepts
980`a` is a map.
982`b` is a map.
984#### Produces
986A map.
988#### Source
990```hoon
991 +- uni
992 ~/ %uni
993 |* b=_a
994 |- ^+ a
995 ?~ b
996 a
997 ?~ a
998 b
999 ?: (vor p.n.a p.n.b)
1000 ?: =(p.n.b p.n.a)
1001 [n.b $(a l.a, b l.b) $(a r.a, b r.b)]
1002 ?: (gor p.n.b p.n.a)
1003 $(a [n.a $(a l.a, b [n.b l.b ~]) r.a], b r.b)
1004 $(a [n.a l.a $(a r.a, b [n.b ~ r.b])], b l.b)
1005 ?: =(p.n.a p.n.b)
1006 [n.b $(b l.b, a l.a) $(b r.b, a r.a)]
1007 ?: (gor p.n.a p.n.b)
1008 $(b [n.b $(b l.b, a [n.a l.a ~]) r.b], a r.a)
1009 $(b [n.b l.b $(b r.b, a [n.a ~ r.a])], a l.a)
1010```
1012#### Examples
1014```
1015 > =m (my [['a' 1] ['b' 2] ~])
1017 > =o (my [['c' 3] ['d' 4] ~])
1019 > (~(uni by m) o)
1020 [n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]
1022 > (~(uni by m) ~)
1023 {[p='a' q=1] [p='b' q=2]}
1025 > n
1026 {[p='a' q=1] [p='c' q=3]}
1027 > (~(uni by o) n)
1028 {[p='d' q=4] [p='a' q=1] [p='c' q=3]}
1029```
1031---
1032### `+-urn:by`
1034Turn (with key)
1036Iterates over every value in map `a` using gate `b`, which accepts both
1037the key and the value of each element as its sample.
1039#### Accepts
1041`a` is a map.
1043`b` is a wet gate that accepts two nouns.
1045#### Produces
1047A noun.
1049#### Source
1051```hoon
1052 +- urn
1053 |* b=$-({* *} *)
1054 |-
1055 ?~ a ~
1056 [n=[p=p.n.a q=(b p.n.a q.n.a)] l=$(a l.a) r=$(a r.a)
1057```
1059#### Examples
1061```
1062 > m
1063 {[p='a' q=1] [p='b' q=2]}
1065 > (~(urn by m) |=(a=(pair * *) q.a))
1066 {[p='a' q=1] [p='b' q=2]}
1068 > (~(urn by m) |=(a=(pair * *) 7))
1069 {[p='a' q=7] [p='b' q=7]}
1071 > (~(urn by m) |=(a=(pair * *) p.a))
1072 {[p='a' q=97] [p='b' q=98]}
1073```
1075---
1077### `+-wyt:by`
1079Depth
1081Produce the depth of the tree map `a`.
1083#### Accepts
1085`a` is a map.
1087#### Produces
1089An atom.
1091#### Source
1093```hoon
1094 +- wyt
1095 |- ^- @
1096 ?~(a 0 +((add $(a l.a) $(a r.a))))
1097```
1099#### Examples
1101```
1102 > =m (my [['a' 1] ['b' 2] ~])
1104 > =o (my [['c' 3] ['d' 4] ~])
1106 > ~(wyt by m)
1109 > ~(wyt by o)
1112 > ~(wyt by (~(uni by m) o))
1114```
1116---
1118### `++ val:by`
1120List of values
1122Produces a list of all values in map `a` that is prepended to list `b`,
1123which is empty by default.
1125#### Accepts
1127`a` is a map.
1129`b` is a list.
1131#### Produces
1133A list.
1135#### Source
1137```hoon
1138++ val
1139 =+ b=`(list _?>(?=(^ a) q.n.a))`~
1140 |- ^+ b
1141 ?~ a b
1142 $(a r.a, b [q.n.a $(a l.a)])
1143```
1145#### Examples
1147```
1148 > =m (my [['a' 1] ['b' 2] ~])
1149 [n=[p='b' q=2] l={[p='a' q=1]} r={}]
1151 > ~(val by m)
1152 ~[1 2]
1153```
1155---