# 2i: Map Logic

### `++by`

Map operations

Container arm for map operation arms. A map is a set of key-value pairs. The contained arms inherit its sample map, `a`.

### Accepts

`a` is a map.

#### Source

``````    ++  by
~/  %by
=|  a=(tree (pair))
=*  node  ?>(?=(^ a) n.a)
``````

#### Examples

``````    > ~(. by (my [%a 1] [%b 2] ~))
< 27.zjr
{   a
?(
{ n/{?(p/\$a p/\$b) q/@ud}
l/nlr({p/?(\$b \$a) q/@ud})
r/nlr({p/?(\$b \$a) q/@ud})
}
\$~
)
<119.wim 31.ohr 1.jmk \$143>
}
``````

### `++all:by`

Logical AND

Computes the logical AND on the results of slamming every element in map `a` with gate `b`.

#### Accepts

`a` is a map.

`b` is a wet gate.

A flag.

#### Source

``````  ++  all                                               ::  logical AND
~/  %all
|*  b=\$-(* ?)
|-  ^-  ?
?~  a
&
?&((b n.a) \$(a l.a) \$(a r.a))
``````

#### Examples

``````    > =b (my [['a' 1] ['b' [2 3]] ~])
> (~(all by b) |=(a=* ?@(a & |)))
%.n

> =a (my [['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
> (~(all by a) |=(a=@ (lte a 6)))
%.y

> (~(all by a) |=(a=@ (lte a 4)))
%.n
``````

### `++any:by`

Logical OR

Computes the logical OR on the results of slamming every element with gate `b`.

#### Accepts

`a` is a map.

`b` is a wet gate.

A flag.

#### Source

``````  ++  any                                               ::  logical OR
~/  %any
|*  b=\$-(* ?)
|-  ^-  ?
?~  a
|
?|((b q.n.a) \$(a l.a) \$(a r.a))
``````

#### Examples

``````    > =b (my [['a' 1] ['b' [2 3]] ~])
> (~(any by b) |=(a=* ?@(a & |)))
%.y

> =a (my [['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5] ~])
> (~(any by a) |=(a=@ (lte a 4)))
%.y
``````

### `++apt:by`

Check correctness

Computes whether `a` has a correct horizontal order and a correct vertical order, producing a flag.

#### Accepts

`a` is a map.

A flag.

#### Source

``````  ++  apt                                               ::  check correctness
=<  \$
~/  %apt
=|  [l=(unit) r=(unit)]
|.  ^-  ?
?~  a   &
?&  ?~(l & &((gor p.n.a u.l) !=(p.n.a u.l)))
?~(r & &((gor u.r p.n.a) !=(u.r p.n.a)))
?~  l.a   &
&((mor p.n.a p.n.l.a) !=(p.n.a p.n.l.a) \$(a l.a, l `p.n.a))
?~  r.a   &
&((mor p.n.a p.n.r.a) !=(p.n.a p.n.r.a) \$(a r.a, r `p.n.a))
==
``````

#### Examples

``````    > =a (my [['a' 1] ['b' [2 3]] ~])
> ~(apt by a)
%.y

> =s (my [[1 2] [3 4] ~])
> =z ?~  s  ~  s(p.n 10)
> z
[n=[p=10 q=2] l={} r={[p=3 q=4]}]

> ~(apt by z)
%.n
``````

#### Discussion

See section `2f` for more information on noun ordering.

### `++bif:by`

Bifurcate

Splits map `a` into two maps `l` and `r`, with the head at `b` discarded.

#### Accepts

`a` is a map.

`b` is a noun.

`c` is a noun.

#### Produces

A cell of two maps.

#### Source

``````  ++  bif                                               ::  splits a by b
~/  %bif
|*  [b=* c=*]
^+  [l=a r=a]
=<  +
|-  ^+  a
?~  a
[[b c] ~ ~]
?:  =(b p.n.a)
?:  =(c q.n.a)
a
a(n [b c])
?:  (gor b p.n.a)
=+  d=\$(a l.a)
?>  ?=(^ d)
d(r a(l r.d))
=+  d=\$(a r.a)
?>  ?=(^ d)
d(l a(r l.d))
``````

#### Examples

``````    > =a (my [['a' 1] ['b' 2] ['c' 3] ~])

> (~(bif by a) ['b' 2])
[l=[n=[p='c' q=3] l=[n=[p='a' q=1] l={} r={}] r=~] r=~]
``````

### `++del:by`

Delete

Produces map `a` with the element located at key `b` removed.

#### Accepts

`a` is a map.

`b` is a key as a noun.

A map.

#### Source

``````  ++  del                                               ::  delete at key b
~/  %del
|*  b=*
|-  ^+  a
?~  a
~
?.  =(b p.n.a)
?:  (gor b p.n.a)
a(l \$(a l.a))
a(r \$(a r.a))
|-  ^-  [\$?(~ _a)]
?~  l.a  r.a
?~  r.a  l.a
?:  (mor p.n.l.a p.n.r.a)
l.a(r \$(l.a r.l.a))
r.a(l \$(r.a l.r.a))
``````

#### Examples

``````    > =b (my [['a' 1] ['b' [2 3]] ~])
> (~(del by b) 'a')
[n=[p='b' q=[2 3]] l=~ r=~]
``````

### `++dig:by`

Produce the address of key `b` within map `a`.

#### Accepts

`a` is a map.

`b` is a key as a noun.

A unit.

#### Source

``````  ++  dig                                               ::  axis of b key
|=  b=*
=+  c=1
|-  ^-  (unit @)
?~  a  ~
?:  =(b p.n.a)  [~ u=(peg c 2)]
?:  (gor b p.n.a)
\$(a l.a, c (peg c 6))
\$(a r.a, c (peg c 7))
``````

#### Examples

``````        > =b (my [['a' 1] ['b' [2 3]] ~])
> (~(dig by b) 'b')
[~ u=2]
``````

### `++gas:by`

Concatenate

Insert a list of key-value pairs `b` into map `a`.

#### Accepts

`a` is a map.

`b` is a list of cells of key-value nouns `p` and `q`.

A map.

#### Source

``````  ++  gas                                               ::  concatenate
~/  %gas
|*  b=(list [p=* q=*])
=>  .(b `(list _?>(?=(^ a) n.a))`b)
|-  ^+  a
?~  b
a
\$(b t.b, a (put p.i.b q.i.b))
``````

#### Examples

``````    > =a (my [['a' 1] ['b' 2] ~])
> =b [['c' 3] ['d' 4] ~]

> (~(gas by a) b)
[n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]
``````

### `++get:by`

Grab unit value

Produce the unit value of the value located at key `b` within map `a`.

#### Accepts

`a` is a map.

`b` is a key as a noun.

A unit.

#### Source

``````  ++  get                                               ::  grab value by key
~/  %get
|*  b=*
=>  .(b `_?>(?=(^ a) p.n.a)`b)
|-  ^-  (unit _?>(?=(^ a) q.n.a))
?~  a
~
?:  =(b p.n.a)
(some q.n.a)
?:  (gor b p.n.a)
\$(a l.a)
\$(a r.a)
``````

#### Examples

``````    > =b (my [['a' 1] ['b' [2 3]] ~])
> (~(get by b) 'b')
[~ u=[2 3]]

> =m (my [['a' 1] ['b' 2] ~])
> (~(get by m) 'a')
[~ u=1]
``````

### `++got:by`

Assert

Produce the value located at key `b` within map `a`. Crash if key `b` does not exist.

#### Accepts

`a` is a map.

`b` is a key as a noun.

A noun.

#### Source

``````  ++  got                                               ::  need value by key
|*  b=*
(need (get b))
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])
> m
[n=[p='b' q=2] l={[p='a' q=1]} r={}]

> (~(got by m) 'a')
1

> (~(got by m) 'c')
! exit
``````

### `++gut:by`

Grab value with default

Produce the value located at key `b` within map `a`. Use default value `c` if key does not exist.

#### Accepts

`a` is a map.

`b` is a key as a noun.

`c` is a noun.

A noun.

#### Source

``````    ++  gut                                               ::  fall value by key
|*  [b=* c=*]
(fall (get b) c)

``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])
> m
[n=[p='b' q=2] l={[p='a' q=1]} r={}]

> (~(gut by m) 'a' 0)
1

> (~(gut by m) 'c' 0)
0
``````

### `++has:by`

Key existence check

Checks whether map `a` contains an element with key `b`, producing a flag.

#### Accepts

`a` is a map.

`b` is a key as a noun.

A flag.

#### Source

``````  ++  has                                               ::  key existence check
~/  %has
|*  b=*
!=(~ (get b))
``````

#### Examples

``````    > =b (my [['a' 1] ['b' [2 3]] ~])
> (~(has by b) 'b')
%.y

> (~(has by b) 'c')
%.n
``````

### `++int:by`

Intersection

Produces a map of the (key) intersection between two maps of the same type, `a` and `b`. If both maps have an identical key that point to different values, the element from map `b` is used.

#### Accepts

`a` is a map.

`b` is a map.

A map.

#### Source

``````  ++  int                                               ::  intersection
~/  %int
=+  b=a
|@
++  \$
|-  ^+  a
?~  b
~
?~  a
~
?:  (mor p.n.a p.n.b)
?:  =(p.n.b p.n.a)
b(l \$(a l.a, b l.b), r \$(a r.a, b r.b))
?:  (gor p.n.b p.n.a)
%-  uni(a \$(a l.a, r.b ~))  \$(b r.b)
%-  uni(a \$(a r.a, l.b ~))  \$(b l.b)
?:  =(p.n.a p.n.b)
b(l \$(b l.b, a l.a), r \$(b r.b, a r.a))
?:  (gor p.n.a p.n.b)
%-  uni(a \$(b l.b, r.a ~))  \$(a r.a)
%-  uni(a \$(b r.b, l.a ~))  \$(a l.a)
--
``````

#### Examples

``````    > =n (my [['a' 1] ['c' 3] ~])
> =m (my [['a' 1] ['b' 2] ~])

> (~(int by m) n)
[n=[p='a' q=1] l=~ r=~]

> =p (my [['a' 2] ['b' 2] ~])
> p
[n=[p='b' q=2] l={[p='a' q=2]} r={}]
> (~(int by p) n)
[n=[p='a' q=1] l=~ r=~]

> =q (my [['a' 2] ['c' 2] ~])
> q
[n=[p='c' q=2] l={[p='a' q=2]} r={}]
> (~(int by p) q)
[n=[p='a' q=2] l=~ r=~]

> =o (my [['c' 3] ['d' 4] ~])
> (~(int by m) o)
~
``````

### `++key:by`

Set of keys

Produces a set of all keys in map `a` that is prepended to set `b`, which is empty by default.

#### Accepts

`a` is a map.

`b` is a set.

A set.

#### Source

``````++  key
=+  b=`(set _?>(?=(^ a) p.n.a))`~
|-  ^+  b
?~  a   b
\$(a r.a, b \$(a l.a, b (~(put in b) p.n.a)))
``````

#### Examples

``````  > =m (my [['a' 1] ['b' 2] ~])
[n=[p='b' q=2] l={[p='a' q=1]} r={}]

> ~(key by m)
{'b' 'a'}
``````

### `++mar:by`

Produces map `a` with the addition of a key-value pair, where the value is a nonempty unit.

Accept a noun and a unit of a noun of the type of the map's keys and values, respectively. Validate that the value is not null and put the pair in the map. If the value is null, delete the key.

#### Accepts

`a` is a map.

`b` is a unit.

#### Produces

A map.

``````  ++  mar                                               ::  add with validation
|*  [b=* c=(unit *)]
?~  c
(del b)
(put b u.c)
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])
> (~(mar by m) 'c' (some 3))
[n=[p='b' q=2] l=[n=[p='c' q=3] l=[n=[p='a' q=1] l={} r={}] r=~] r=~]

> (~(mar by m) 'c' ~)
[n=[p='b' q=2] l=[n=[p='a' q=1] l=~ r=~] r=~]

> (~(mar by m) 'b' ~)
[n=[p='a' q=1] l=~ r=~]
``````

### `++put:by`

Produces `a` with the addition of the key-value pair of `b` and `c`.

#### Accepts

`a` is a map.

`b` is a key of the same type as the keys in `a`.

`c` is a value of the same type of the values in `a`.

A map.

#### Source

``````  ++  put                                               ::  adds key-value pair
~/  %put
|*  [b=* c=*]
|-  ^+  a
?~  a
[[b c] ~ ~]
?:  =(b p.n.a)
?:  =(c q.n.a)
a
a(n [b c])
?:  (gor b p.n.a)
=+  d=\$(a l.a)
?>  ?=(^ d)
?:  (mor p.n.a p.n.d)
a(l d)
d(r a(l r.d))
=+  d=\$(a r.a)
?>  ?=(^ d)
?:  (mor p.n.a p.n.d)
a(r d)
d(l a(r l.d))
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])
> (~(put by ~) 'c' 3)
[n=[p='b' q=2] l=[n=[p='c' q=3] l={[p='a' q=1]} r={}] r=~]

> (~(put by m) "zod" 26)
! nest-fail
! exit

> (~(put by m) 'a' 2)
[n=[p='b' q=2] l=[n=[p='a' q=2] l={} r={}] r=~]

> (put:by m 'a' 2)
{[p=[[98 2] [[97 1] 0 0] 0] q=[97 2]]}
``````

### `++rep:by`

Replace by product

Accumulate elements of map `a` using gate `b`, producing a noun.

#### Accepts

`a` is a map.

`b` is a wet gate.

A noun.

#### Source

``````  ++  rep                                               ::  reduce to product
~/  %rep
|*  b=_=>(~ |=([* *] +<+))
|-
?~  a  +<+.b
\$(a r.a, +<+.b \$(a l.a, +<+.b (b n.a +<+.b)))
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])
> (~(rep by m) |=(a=(pair * *) p.a))
q=[97 1]
``````

### `++rib:by`

Replace values with accumulator

#### Accepts

`a` is a map.

`b` is a noun.

`c` is a wet gate.

A cell of nouns.

#### Source

``````  ++  rib                                               ::  transform + product
|*  [b=* c=gate]
|-  ^+  [b a]
?~  a  [b ~]
=+  d=(c n.a b)
=.  n.a  +.d
=+  e=\$(a l.a, b -.d)
=+  f=\$(a r.a, b -.e)
[-.f a(l +.e, r +.f)]
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])
> (~(rib by m) [1 2] (con by m))

> (~(rep by m) [1 2] |=(a=(pair * *) p.a))
``````

### `++run:by`

Transform values

Iterates over every value in map `a` using gate `b`, producing a map.

#### Accepts

`a` is a map.

`b` is a wet gate.

A map.

#### Source

``````  ++  run                                               ::  apply gate to values
~/  %run
|*  b=gate
|-
?~  a  a
[n=[p=p.n.a q=(b q.n.a)] l=\$(a l.a) r=\$(a r.a)]
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])

> ^+(m (~(run by m) dec))
[n=[p='b' q=1] l={[p='a' q=0]} r={}]

> `(map @tas @t)`(~(run by m) (cury scot %ux))
{[p=%a q='0x1'] [p=%b q='0x2']}
``````

### `++rut:by`

Transform nodes

Applies a gate `b` to nodes in map `a`.

#### Accepts

`a` is a map.

`b` is a wet gate.

A map.

#### Source

``````  ++  rut                                               ::  apply gate to nodes
|*  b=gate
|-
?~  a  a
[n=[p=p.n.a q=(b p.n.a q.n.a)] l=\$(a l.a) r=\$(a r.a)]
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])
``````

### `++tap:by`

Listify pairs

Produces the list of all elements in map `a` that is prepended to list `b`, which is empty by default.

#### Accepts

`a` is a map.

`b` is a list.

A list.

#### Source

``````  ++  tap                                               ::  listify pairs
=<  \$
~/  %tap
=+  b=`(list _?>(?=(^ a) n.a))`~
|.  ^+  b
?~  a
b
\$(a r.a, b [n.a \$(a l.a)])
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])
[n=[p='b' q=2] l={[p='a' q=1]} r={}]
> `*`m
[[98 2] [[97 1] 0 0] 0]

> ~(tap by m)
~[[p='b' q=2] [p='a' q=1]]
> `*`~(tap by m)
[[98 2] [97 1] 0]
``````

### `++uni:by`

Union

Produces a map of the union between the keys of `a` and `b`. If `b` shares a key with `a`, the tuple from `b` is preserved.

#### Accepts

`a` is a map.

`b` is a map.

A map.

#### Source

``````  ++  uni                                               ::  union, merge
~/  %uni
=+  b=a
|@
++  \$
|-  ^+  a
?~  b
a
?~  a
b
?:  =(p.n.b p.n.a)
b(l \$(a l.a, b l.b), r \$(a r.a, b r.b))
?:  (mor p.n.a p.n.b)
?:  (gor p.n.b p.n.a)
\$(l.a \$(a l.a, r.b ~), b r.b)
\$(r.a \$(a r.a, l.b ~), b l.b)
?:  (gor p.n.a p.n.b)
\$(l.b \$(b l.b, r.a ~), a r.a)
\$(r.b \$(b r.b, l.a ~), a l.a)
--
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])

> =o (my [['c' 3] ['d' 4] ~])

> (~(uni by m) o)
[n=[p='b' q=2] l=[n=[p='d' q=4] l={} r={[p='a' q=1] [p='c' q=3]}] r=~]

> (~(uni by m) ~)
{[p='a' q=1] [p='b' q=2]}

> n
{[p='a' q=1] [p='c' q=3]}
> (~(uni by o) n)
{[p='d' q=4] [p='a' q=1] [p='c' q=3]}
``````

### `++urn:by`

Turn (with key)

Iterates over every value in map `a` using gate `b`, which accepts both the key and the value of each element as its sample.

#### Accepts

`a` is a map.

`b` is a wet gate that accepts two nouns.

A noun.

#### Source

``````  ++  urn                                               ::  apply gate to nodes
~/  %urn
|*  b=\$-([* *] *)
|-
?~  a  ~
a(n n.a(q (b p.n.a q.n.a)), l \$(a l.a), r \$(a r.a))
``````

#### Examples

``````    > m
{[p='a' q=1] [p='b' q=2]}

> (~(urn by m) |=(a=(pair * *) q.a))
{[p='a' q=1] [p='b' q=2]}

> (~(urn by m) |=(a=(pair * *) 7))
{[p='a' q=7] [p='b' q=7]}

> (~(urn by m) |=(a=(pair * *) p.a))
{[p='a' q=97] [p='b' q=98]}
``````

### `++wyt:by`

Depth

Produce the depth of the tree map `a`.

#### Accepts

`a` is a map.

An atom.

#### Source

``````  ++  wyt                                               ::  depth of map
=<  \$
~%  %wyt  +  ~
|.  ^-  @
?~(a 0 +((add \$(a l.a) \$(a r.a))))
``````

#### Examples

``````    > =m (my [['a' 1] ['b' 2] ~])

> =o (my [['c' 3] ['d' 4] ~])

> ~(wyt by m)
2

> ~(wyt by o)
2

> ~(wyt by (~(uni by m) o))
4
``````

### `++val:by`

List of values

Produces a list of all values in map `a` that is prepended to list `b`, which is empty by default.

#### Accepts

`a` is a map.

`b` is a list.

A list.

#### Source

``````++  val
=+  b=`(list _?>(?=(^ a) q.n.a))`~
|-  ^+  b
?~  a   b
\$(a r.a, b [q.n.a \$(a l.a)])
``````

#### Examples

``````  > =m (my [['a' 1] ['b' 2] ~])
[n=[p='b' q=2] l={[p='a' q=1]} r={}]

> ~(val by m)
~[1 2]
``````