2m: Ordered Maps

+mop

Ordered map mold builder

Constructs and validates an ordered map based on key type, value type and a comparator gate.

Ordinary +maps are always ordered by the +mug hash of their keys, while +mops are ordered by a comparator gate of your choosing.

Accepts

+mop has two layers, the first is a wet gate that takes two molds:

  • key is a $mold, the type of the map key.

  • value is a $mold, the type of the map value.

The wet gate produces a dry gate that takes:

  • ord is a binary comparator $gate of $-([key key] ?) used to determine item ordering. It produces %.y if the first key should be first, and %.n if it should be second.

Produces

A $mold.

Source

Examples

Descending order:

Ascending order:

Molding a correctly ordered mop

Molding an incorrectly ordered mop:

Note you can't use ordinary +map constructors like +malt to make a +mop as it'll be in the wrong order.


+ordered-map

A synonym for +on.

Source


+on

Ordered map operations

Container arm for +mop operation arms. A +mop is an ordered set of key-value pairs.

Accepts

+on has two layers, the first is a wet gate that takes two molds:

  • key is a $mold, the type of the map keys.

  • val is a $mold, the type of the map values.

The wet gate produces a dry gate that takes:

  • compare is a binary comparator $gate of $-([key key] ?) used to determine item ordering. It produces %.y if the first key should be first, and %.n if it should be second.

Produces

A +core whose arms perform the various +mop operations.

Source

Examples

Here are the core's arms:


+all:on

Apply logical AND boolean test on all items.

Accepts

.a is a +mop.

.b is a $gate of the type $-([key val] ?), where the type of key and val match those of the +mop.

Produces

A ?.

Source

Examples


+any:on

Apply logical OR boolean test on all items.

Accepts

.a is a +mop.

.b is a $gate of the type $-([key val] ?), where the type of key and val match those of the +mop.

Produces

A ?.

Source

Examples


+apt:on

Verify horizontal and vertical orderings.

Accepts

.a is a +mop.

Produces

A ?.

Source

Examples

Incorrect order:

Correct order:


+bap:on

Convert to list, right to left

Accepts

.a is a +mop.

Produces

A (list [key val]), where key and val are the types of the keys and values in the +mop.

Source

Examples


+del:on

Delete an item from a +mop if it exists, producing its value if it's deleted, and a new +mop.

Accepts

.a is a +mop.

.b is a noun, the type of the keys in .a.

Produces

A cell of a (unit val) and a +mop.

Source

Examples


+dip:on

Stateful partial inorder traversal

Mutates $state on each run of gate .f. Traverses from left to right. Stops when .f produces stop=%.y, or else runs all the way to the end. Each run of .f can replace an item's value or delete the item.

Accepts

+dip is a wet gate that takes .state, which is a $mold. The wet gate produces a dry gate that takes:

  • .a is a +mop.

  • .state is the initial value for the state.

  • .f is a gate of the type $-([state item] [(unit val) stop=? state]). It takes a pair of the current state and a key-value pair from the +mop. It produces a triple of:

    • (unit val) a new value for the current item. If it is null, the item is deleted. If you don't want to modify the value, you just give it back the same one you received.

    • .stop is %.y if traversal should end here, and %.n if it should continue.

    • .state is a new value for the .state.

Produces

A cell of the final .state and the new, possibly modified, +mop.

Source

Examples

Add them all up:

Add them up, stopping when the key is less than 3:

Delete items less than three:


+gas:on

Put a list of key-value pairs in a +mop.

Accepts

.a is a +mop.

.b is a list of key-value pairs, whose types match those of the +mop.

Source

Examples


+get:on

Get a value at a key or null

Accepts

.a is a +mop.

.b is a noun whose type matches that of the mop's keys.

Produces

A (unit val), where +val is the type of the values of the +mop.

Source

Examples


+got:on

Get a value at a key, crashing if it doesn't exist

Accepts

.a is a +mop.

.b is a noun whose type matches that of the mop's keys.

Produces

A noun of the type of the values of the +mop, crashing if the key doesn't exist.

Source

Examples


+has:on

Check for key existence

Accepts

.a is a +mop.

.b is a noun whose type matches that of the mop's keys.

Produces

A ?.

Source

Examples


+lot:on

Take a subset range excluding +start and/or +end, and all elements outside the range.

Accepts

tre is a +mop.

+start is a (unit key), where key is a noun whose type matches the type of the +mop keys. If non-null, this item and all those previous will be excluded.

+end is a (unit key), where key is a noun whose type matches the type of the +mop keys. If non-null, this item and all those after will be excluded.

Produces

A +mop.

Source

Examples


+nip:on

Remove root (for internal use)

Accepts

.a is a +mop.

Produces

A +mop.

Source

Examples

Note this is for internal use, you would not normally use +nip.


+pop:on

Produce +head (the leftmost item) and +rest or crash if empty

Accepts

.a is a +mop.

Produces

A cell of +head, the leftmost item, and +rest, the +mop sans its leftmost item. If the +mop was empty, it'll crash.

Source

Examples


+pry:on

Produce the head (leftmost item) or null

Accepts

.a is a +mop.

Produces

A (unit item), where $item is a key-value pair. The unit is null if the mop was empty.

Source

Examples


+put:on

Insert an item

Accepts

.a is a +mop.

key is a noun whose type matches the keys in .a.

val is a noun whose type matches the values in .a.

Produces

A +mop.

Source

Examples


+ram:on

Produce tail (rightmost item) or null

Accepts

.a is a +mop.

Produces

A (unit item) where $item is a key-value pair whose type is that of the keys and values in the +mop.

Source

Examples


+run:on

Apply gate to transform all values in place

Accepts

.a is a +mop.

.b is a gate of $-(val *), where +val is the type of the values in the +mop.

Produces

A +mop.

Source

Examples


+tab:on

Tabulate a subset with a max count, maybe starting after a certain element.

Accepts

.a is a +mop.

.b is a (unit key). The type of key matches the type of keys in the +mop. This specifies where to start if non-null.

.c is a @ specifying the maximum number of items to return.

Produces

A (list item), where $item is a key-value matching the type of the +mop.

Source

Examples


+tap:on

Convert to list, left to right

Accepts

.a is a +mop.

Produces

A (list item), where $item is a key-value pair whose type matches those of the +mop.

Source

Examples

+uni:on

Unify two ordered maps

If the keys overlap and the values are different, the value in mop .b take precedence over the value in mop .a.

Accepts

.a is a +mop.

.b is a +mop.

Produces

A +mop.

Source

Examples


Last updated