# 2i: Map Logic

## `+by` <a href="#by" id="by"></a>

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

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

#### Examples

```
> ~(. by (malt (limo ~[a+1 b+2 c+3])))
< 27.jus
  [   a
    ?(
      %~
      [ n=[?(p=%a p=%b p=%c) q=@ud]
        l=nlr([p=?(%a %b %c) q=@ud])
        r=nlr([p=?(%a %b %c) q=@ud])
      ]
    )
    <123.zao 46.hgz 1.pnw %140>
  ]
>
```

***

### `+all:by` <a href="#allby" id="allby"></a>

Logical AND.

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

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a `$gate`.

#### Produces

A `$flag`.

#### Source

```hoon
++  all
  ~/  %all
  |*  b=$-(* ?)
  |-  ^-  ?
  ?~  a
    &
  ?&((b q.n.a) $(a l.a) $(a r.a))
```

#### Examples

```
> =a (malt (limo ~[a+1 b+[2 3]]))

> (~(all by a) |=(a=* ?@(a & |)))
%.n
```

```
> =a (malt (limo ~[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` <a href="#anyby" id="anyby"></a>

Logical OR.

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

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a wet `$gate`.

#### Produces

A `$flag`.

#### Source

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

#### Examples

```
> =a (malt (limo ~[a+1 b+[2 3]]))

> (~(any by a) |=(a=* ?@(a & |)))
%.y
```

```
> =a (malt (limo ~[a+1 b+2 c+3 d+4 e+5]))

> (~(any by a) |=(a=@ (lte a 4)))
%.y
```

***

### `+apt:by` <a href="#aptby" id="aptby"></a>

Check correctness.

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

#### Accepts

`.a` is a `+map`.

#### Produces

A `$flag`.

#### Source

```hoon
++  apt
  =<  $
  ~/  %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 (malt `(list [@tas @])`~[a+1 b+2 c+3 d+4 e+5])

> ~(apt by a)
%.y

> =z ?~(a ~ a(p.n `@tas`%z))

> z
[n=[p=%z q=2] l={[p=%e q=5]} r={[p=%d q=4] [p=%a q=1] [p=%c q=3]}]

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

#### Discussion

See section [`2f`](/hoon/stdlib/2f.md) for more information on `$noun` ordering.

### `+bif:by` <a href="#bifby" id="bifby"></a>

Bifurcate.

Splits `+map` `.a` into two `+map`s `.l` and `.r`, which contain the items either side of key `.b` with value `.c` but not including the pair of key `.b` and value `.c`.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a `$noun`.

`.c` is a `$noun`.

#### Produces

A cell of two `+map`s.

#### Source

```hoon
++  bif
  ~/  %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 (malt `(list [@tas @])`~[a+1 b+2 c+3 d+4 e+5])

> (~(bif by a) b+2)
[l=[n=[p=%e q=5] l=~ r=~] r=[n=[p=%d q=4] l=~ r=[n=[p=%c q=3] l={[p=%a q=1]} r={}]]]

> `[(map @tas @) (map @tas @)]`(~(bif by a) b+2)
[{[p=%e q=5]} {[p=%d q=4] [p=%a q=1] [p=%c q=3]}]
```

#### Discussion

Note that `+map`s are horizontally ordered by the [`+mug`](/hoon/stdlib/2e.md#mug) hash of their keys and vertically ordered by the double-`+mug` hash of their keys. This means bifurcating the `+map` `(malt ~[10^10 20^20 30^30 40^40 50^50])` at `30^30` will not produce `[{10^10 20^20} {40^40 50^50}]`, but rather `[{20^20} {10^10 40^40 50^50}]` due to the tree structure resulting from their `+mug` hashes.

***

### `+del:by` <a href="#delby" id="delby"></a>

Delete.

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

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a key as a `$noun`.

#### Produces

A `+map`.

#### Source

```hoon
++  del
  ~/  %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

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(del by a) %z)
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(del by a) %b)
{[p=%d q=4] [p=%a q=1] [p=%c q=3]}
```

***

### `+dif:by` <a href="#difby" id="difby"></a>

Difference.

Computes the difference between `.a` and `.b`, producing the `+map` of key-value pairs in `.a` whose keys are not in `.b`.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a `+map`.

#### Produces

A `+map`.

#### Source

```hoon
++  dif
  ~/  %dif
  =+  b=a
  |@
  ++  $
    |-  ^+  a
    ?~  b
      a
    =+  c=(bif p.n.b q.n.b)
    ?>  ?=(^ c)
    =+  d=$(a l.c, b l.b)
    =+  e=$(a r.c, b r.b)
    |-  ^-  [$?(~ _a)]
    ?~  d  e
    ?~  e  d
    ?:  (mor p.n.d p.n.e)
      d(r $(d r.d))
    e(l $(e l.e))
  --
```

#### Examples

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> =b `(map @tas @)`(malt (limo ~[c+3 d+4 e+5 f+6]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> b
{[p=%e q=5] [p=%d q=4] [p=%f q=6] [p=%c q=3]}

> `(map @tas @)`(~(dif by a) b)
{[p=%b q=2] [p=%a q=1]}
```

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> =b `(map @tas @)`(malt (limo ~[a+2 e+4 f+5]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> b
{[p=%e q=4] [p=%f q=5] [p=%a q=2]}

> `(map @tas @)`(~(dif by a) b)
{[p=%b q=2] [p=%d q=4] [p=%c q=3]}
```

#### Discussion

This only compares keys, so if both `+map`s contain the same key with different values, that key-value pair is not considered a difference and will not be included in the resulting `+map`.

***

### `+dig:by` <a href="#digby" id="digby"></a>

Address of key-value pair.

Produce the address of key-value pair for key `.b` within `+map` `.a`.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a key as a `$noun`.

#### Produces

A unit.

#### Source

```hoon
++  dig
  |=  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

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> (~(dig by a) %a)
[~ 252]

> (~(dig by a) %b)
[~ 2]

> (~(dig by a) %e)
~
```

***

### `+gas:by` <a href="#gasby" id="gasby"></a>

Merge.

Like Clojure's `merge`. Insert a list of key-value pairs `.b` into `+map` `.a`. For a key which exists in both `.a` and `.b`, the value is replaced with the value in `.b`.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a list of cells of key-value `$noun`s `.p` and `.q`.

#### Produces

A `+map`.

#### Source

```hoon
++  gas
  ~/  %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 `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(gas by a) ~[e+5 f+6 g+7])
{[p=%e q=5] [p=%b q=2] [p=%d q=4] [p=%f q=6] [p=%g q=7] [p=%a q=1] [p=%c q=3]}
```

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2]))

> a
{[p=%b q=2] [p=%a q=1]}

> `(map @tas @)`(~(gas by a) ~[a+100 b+200])
{[p=%b q=200] [p=%a q=100]}
```

```
> `(map @tas @)`(~(gas by `(map @tas @)`~) ~[a+100 b+200])
{[p=%b q=200] [p=%a q=100]}
```

***

### `+get:by` <a href="#getby" id="getby"></a>

Grab unit value.

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

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a key as a `$noun`.

#### Produces

A unit.

#### Source

```hoon
++  get
  ~/  %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

```
> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(get by a) %a)
[~ 1]

> (~(get by a) %b)
[~ 2]

> (~(get by a) %z)
~
```

***

### `+got:by` <a href="#gotby" id="gotby"></a>

Assert.

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

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a key as a `$noun`.

#### Produces

A `$noun`.

#### Source

```hoon
++  got
  |*  b=*
  (need (get b))
```

#### Examples

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(got by a) %a)
1

> (~(got by a) %b)
2

> (~(got by a) %z)
dojo: hoon expression failed
```

***

### `+gut:by` <a href="#gutby" id="gutby"></a>

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`, and is the sample of `+by`.

`.b` is a key as a `$noun`.

`.c` is a `$noun`.

#### Produces

A `$noun`.

#### Source

```hoon
++  gut
  |*  [b=* c=*]
  (fall (get b) c)
```

#### Examples

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(gut by a) %a 9.999)
1

> (~(gut by a) %b 9.999)
2

> (~(gut by a) %z 9.999)
9.999
```

***

### `+has:by` <a href="#hasby" id="hasby"></a>

Key existence check.

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

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a key as a `$noun`.

#### Produces

A `$flag`.

#### Source

```hoon
++  has
  ~/  %has
  |*  b=*
  !=(~ (get b))
```

#### Examples

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(has by a) %a)
%.y

> (~(has by a) %z)
%.n
```

***

### `+int:by` <a href="#intby" id="intby"></a>

Intersection.

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

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a `+map`.

#### Produces

A `+map`.

#### Source

```hoon
++  int
  ~/  %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

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> =b `(map @tas @)`(malt (limo ~[c+3 d+4 e+5 f+6]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> b
{[p=%e q=5] [p=%d q=4] [p=%f q=6] [p=%c q=3]}

> `(map @tas @)`(~(int by a) b)
{[p=%d q=4] [p=%c q=3]}
```

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2]))

> =b `(map @tas @)`(malt (limo ~[a+100 b+200]))

> a
{[p=%b q=2] [p=%a q=1]}

> b
{[p=%b q=200] [p=%a q=100]}

> `(map @tas @)`(~(int by a) b)
{[p=%b q=200] [p=%a q=100]}
```

***

### `+jab:by` <a href="#jabby" id="jabby"></a>

Transform value.

Produce `+map` `.a` with the value at key `.b` transformed by `$gate` `.c`.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a `$noun`, and a key in `.a`.

`.c` is a `$gate`.

#### Produces

A `+map`.

#### Source

```hoon
++  jab
  ~/  %jab
  |*  [key=_?>(?=(^ a) p.n.a) fun=$-(_?>(?=(^ a) q.n.a) _?>(?=(^ a) q.n.a))]
  ^+  a
  ::
  ?~  a  !!
  ::
  ?:  =(key p.n.a)
    a(q.n (fun q.n.a))
  ::
  ?:  (gor key p.n.a)
    a(l $(a l.a))
  ::
  a(r $(a r.a))
```

#### Examples

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(jab by a) %d |=(x=@ (pow x 2)))
{[p=%b q=2] [p=%d q=16] [p=%a q=1] [p=%c q=3]}

> (~(jab by a) %z |=(x=@ (pow x 2)))
dojo: hoon expression failed

> (~(jab by a) %d |=(a=@ [a a]))
-need.?(%~ [n=[p=@tas q=@] l=nlr([p=@tas q=@]) r=nlr([p=@tas q=@])])
-have.[n=[p=@tas q=[@ @]] l=nlr([p=@tas q=@]) r=nlr([p=@tas q=@])]
nest-fail
dojo: hoon expression failed
```

***

### `+key:by` <a href="#keyby" id="keyby"></a>

Set of keys.

Produces a `+set` of all keys in `+map` `.a`.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

#### Produces

A `+set`.

#### Source

```hoon
++  key
  =<  $
  ~/  %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

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> ~(key by a)
{%b %d %a %c}
```

***

### `+mar:by` <a href="#marby" id="marby"></a>

Add with validation.

Produces `+map` `.a` with the addition of key-value pair `.b` and `.c`, 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`, and is the sample of `+by`.

`.b` is a `$noun`.

`.c` is a `+unit`.

#### Produces

A `+map`.

#### Source

```hoon
++  mar
  |*  [b=* c=(unit *)]
  ?~  c
    (del b)
  (put b u.c)
```

#### Examples

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(mar by a) %e (some 5))
{[p=%e q=5] [p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(mar by a) %a (some 10))
{[p=%b q=2] [p=%d q=4] [p=%a q=10] [p=%c q=3]}

> `(map @tas @)`(~(mar by a) %a ~)
{[p=%b q=2] [p=%d q=4] [p=%c q=3]}
```

***

### `+put:by` <a href="#putby" id="putby"></a>

Add key-value pair.

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

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.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`.

#### Produces

A `+map`.

#### Source

```hoon
++  put
  ~/  %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

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3]))

> a
{[p=%b q=2] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(put by a) %d 4)
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> `(map @tas @)`(~(put by a) %a 10)
{[p=%b q=2] [p=%a q=10] [p=%c q=3]}

> (~(put by a) 42 'foo')
mull-grow
mull-nice
-need.?(%~ [n=[p=@tas q=@] l=nlr([p=@tas q=@]) r=nlr([p=@tas q=@])])
-have.[[@ud @t] %~ %~]
nest-fail
dojo: hoon expression failed
```

***

### `+rep:by` <a href="#repby" id="repby"></a>

Reduce to product.

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

#### Accepts

`.a` is a `+map`.

`.b` is a `$gate`.

#### Produces

A `$noun`.

#### Source

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

#### Examples

```
> =a `(map @tas @)`(malt (limo ~[a+1 b+2 c+3 d+4]))

> a
{[p=%b q=2] [p=%d q=4] [p=%a q=1] [p=%c q=3]}

> (~(rep by a) |=([p=[@tas @] q=@] ~&([p q] (add +.p q))))
[[%b 2] 0]
[[%d 4] 2]
[[%c 3] 6]
[[%a 1] 9]
q=10
```

#### Discussion

The `$gate` will iteratively be fed a cell whose head is a key-value pair from the `+map` and whose tail is an accumulator, producing the final value of the accumulator.

***

### `+rib:by` <a href="#ribby" id="ribby"></a>

Transform + product.

`.c` is a `$gate` with a sample like `[[key value] accumulator]` and a product like `[accumulator [key value]]`. Each key-value pair in `+map` `.a` is passed to `.c` and replaced with the key-value pair `.c` produced. The final value of the accumulator and the modified `+map` are returned. `.b` is the initial value of the accumulator.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a `$noun`, and is the initial value of the accumulator.

`.c` is a `$gate`.

#### Produces

A cell of a `$noun` and a `+map`.

#### Source

```hoon
++  rib
  |*  [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

In this example, all values less than three are changed to zero, and a list of their keys are produced along with the modified `+map`.

```
> =a `(map @t @)`(malt ~[['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5]])

> a
{[p='e' q=5] [p='b' q=2] [p='d' q=4] [p='a' q=1] [p='c' q=3]}

> =c |=  [[k=@t v=@] acc=(list @t)]
     ?:  (lth v 3)
       [[k acc] [k 0]]
     [acc [k v]]

> `[(list @t) (map @t @)]`(~(rib by a) *(list @t) c)
[<|a b|> {[p='e' q=5] [p='b' q=0] [p='d' q=4] [p='a' q=0] [p='c' q=3]}]
```

#### Discussion

Key-value pairs in the `+map` are transformed in their existing tree location. This means if you change the key, you'd likely produce a `+map` with an incorrect order, so typically you should only change the value.

***

### `+run:by` <a href="#runby" id="runby"></a>

Transform values.

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

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a `$gate`.

#### Produces

A `+map`.

#### Source

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

#### Examples

```
> =a `(map @t @)`(malt ~[['a' 1] ['b' 2] ['c' 3] ['d' 4] ['e' 5]])

> `(map @t @)`(~(run by a) dec)
{[p='e' q=4] [p='b' q=1] [p='d' q=3] [p='a' q=0] [p='c' q=2]}
```

***

### `+tap:by` <a href="#tapby" id="tapby"></a>

Listify pairs.

Produces the list of all elements in `+map` `.a`.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

#### Produces

A list.

#### Source

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

#### Examples

```
> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3] [4 4] [5 5]])

> ~(tap by a)
~[[p=4 q=4] [p=3 q=3] [p=2 q=2] [p=1 q=1] [p=5 q=5]]
```

***

### `+uni:by` <a href="#uniby" id="uniby"></a>

Union, merge.

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`, and is the sample `+by`.

`.b` is a `+map`.

#### Produces

A `+map`.

#### Source

```hoon
++  uni
  ~/  %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

```
> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3]])

> =b `(map @ @)`(malt ~[[3 300] [4 400] [5 500]])

> a
{[p=1 q=1] [p=2 q=2] [p=3 q=3]}

> b
{[p=5 q=500] [p=3 q=300] [p=4 q=400]}

> `(map @ @)`(~(uni by a) b)
{[p=5 q=500] [p=1 q=1] [p=2 q=2] [p=3 q=300] [p=4 q=400]}
```

***

### `+uno:by` <a href="#unoby" id="unoby"></a>

General union.

Produces a `+map` of the union between the keys of `.a` and `.b`. If `.b` shares a key with `.a`, `$gate` `+meg` is applied to both and its product is used as the new value of the key in question.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

`.b` is a `+map`, and is the sample of `~(uno by a)`.

`+meg` is a `$gate`, and is the sample of `(~(uno by a) b)`.

#### Produces

A `+map`.

#### Source

```hoon
++  uno
  =+  b=a
  |@
  ++  $
    |=  meg=$-([_p:node _q:node _q:node] _q:node)
    |-  ^+  a
    ?~  b
      a
    ?~  a
      b
    ?:  =(p.n.b p.n.a)
      :+  [p.n.a (meg p.n.a q.n.a q.n.b)]
        $(b l.b, a l.a)
      $(b r.b, a r.a)
    ?:  (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

```
> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3]])

> =b `(map @ @)`(malt ~[[3 3] [4 4] [5 5]])

> a
{[p=1 q=1] [p=2 q=2] [p=3 q=3]}

> b
{[p=5 q=5] [p=3 q=3] [p=4 q=4]}

> `(map @ @)`((~(uno by a) b) |=([k=@ v=@ w=@] (add v w)))
{[p=5 q=5] [p=1 q=1] [p=2 q=2] [p=3 q=6] [p=4 q=4]}
```

***

### `+urn:by` <a href="#urnby" id="urnby"></a>

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 `$gate` that accepts two `$noun`s.

#### Produces

A `+map`.

#### Source

```hoon
++  urn
  ~/  %urn
  |*  b=$-([* *] *)
  |-
  ?~  a  ~
  a(n n.a(q (b p.n.a q.n.a)), l $(a l.a), r $(a r.a))
```

#### Examples

```
> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3]])

> a
{[p=1 q=1] [p=2 q=2] [p=3 q=3]}

> (~(urn by a) |=([k=@ v=@] (pow v 2)))
{[p=1 q=1] [p=2 q=4] [p=3 q=9]}
```

***

### `+wyt:by` <a href="#wytby" id="wytby"></a>

Depth.

Produce the size of the tree `+map` `.a`.

#### Accepts

`.a` is a `+map`, and is the sample of `+by`.

#### Produces

An `$atom`.

#### Source

```hoon
++  wyt
  =<  $
  ~%  %wyt  +  ~
  |.  ^-  @
  ?~(a 0 +((add $(a l.a) $(a r.a))))
```

#### Examples

```
> =a `(map @ @)`(malt ~[[1 1] [2 2] [3 3]])

> =b `(map @ @)`(malt ~[[1 1] [2 2] [3 3] [4 4] [5 5]])

> a
{[p=1 q=1] [p=2 q=2] [p=3 q=3]}

> b
{[p=5 q=5] [p=1 q=1] [p=2 q=2] [p=3 q=3] [p=4 q=4]}

> ~(wyt by a)
3

> ~(wyt by b)
5
```

***

### `+val:by` <a href="#valby" id="valby"></a>

List of values.

Produces a list of all values in `+map` `.a`.

#### Accepts

`.a` is a `+map`.

#### Produces

A list.

#### Source

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

#### Examples

```
> =a `(map @t @)`(malt ~[['a' 1] ['b' 2] ['c' 3]])

> a
{[p='b' q=2] [p='a' q=1] [p='c' q=3]}

> ~(val by a)
~[3 1 2]
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.urbit.org/hoon/stdlib/2i.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
