Skip to content

Latest commit

 

History

History
1407 lines (949 loc) · 22.2 KB

2i.md

File metadata and controls

1407 lines (949 loc) · 22.2 KB
description Functions for working with key-value `+map`s, including insertion, deletion, lookup, and `+map` transformations.
layout
title description tableOfContents outline pagination
visible
true
visible
visible
true
visible
true
visible
true

2i: Map Logic

+by {#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))  ::  (map)
  |@

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 {#allby}

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

++  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 {#anyby}

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

++  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 {#aptby}

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

++  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 for more information on $noun ordering.

+bif:by {#bifby}

Bifurcate.

Splits +map .a into two +maps .l and .r, which contain the items either side of key .b, not including the item at key .b.

Accepts

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

.b is a $noun, the key to split at.

Produces

A cell of two +maps.

Source

++  bif
  ~/  %bif
  |*  b=*
  |-  ^+  [l=a r=a]
  ?~  a
    [~ ~]
  ?:  =(b p.n.a)
    +.a
  ?:  (gor b p.n.a)
    =+  d=$(a l.a)
    ?>  ?=(^ d)
    [l.d a(l r.d)]
  =+  d=$(a r.a)
  ?>  ?=(^ d)
  [a(r l.d) r.d]

Examples

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

> (~(bif by a) %b)
[ 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)
[{[p=%e q=5]} {[p=%d q=4] [p=%a q=1] [p=%c q=3]}]

Discussion

Note that +maps are horizontally ordered by the +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.

+bif:by formerly took two arguments, a key .b and a value .c, and inserted that pair if it was absent. It now takes the key alone and never inserts. Because the sample is b=*, calling it the old way does not raise an error — the two arguments are passed as the single cell [b c], so the map is split at that cell rather than at the key, and the item at .b is left in the result. Callers written against the old signature therefore fail silently. For example:

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

The map is split on the noun [%b 2], and [p=%b q=2] is still present in the result rather than having been removed.


+del:by {#delby}

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

++  del
  ~/  %del
  |*  b=*
  =>  .(b `_?>(?=(^ a) p.n.a)`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 {#difby}

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

++  dif
  ~/  %dif
  |*  b=_a
  |-  ^+  a
  ?~  b
    a
  =+  c=(bif p.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 +maps 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 {#digby}

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

++  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 {#gasby}

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 $nouns .p and .q.

Produces

A +map.

Source

++  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 {#getby}

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

++  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 {#gotby}

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

++  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 {#gutby}

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

++  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 {#hasby}

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

++  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 {#intby}

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

.b is a +map.

Produces

A +map.

Source

++  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 {#jabby}

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

++  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 {#keyby}

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

++  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 {#marby}

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

++  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 {#putby}

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

++  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 {#repby}

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

++  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 {#ribby}

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

++  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 {#runby}

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

++  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 {#tapby}

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

++  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 {#uniby}

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

++  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 {#unoby}

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

++  uno
  |*  b=_a
  |*  meg=$-([* * *] *)
  |-  ^+  a
  ?~  b
    a
  ?~  a
    b
  ?:  =(p.n.b p.n.a)
    :+  [p.n.a `_?>(?=(^ a) q.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 {#urnby}

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 $nouns.

Produces

A +map.

Source

++  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 {#wytby}

Depth.

Produce the size of the tree +map .a.

Accepts

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

Produces

An $atom.

Source

++  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 {#valby}

List of values.

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

Accepts

.a is a +map.

Produces

A list.

Source

++  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]