2f: Noun Ordering

+aor

Alphabetical order.

Computes whether .a and .b are in alphabetical order, producing a $flag. Orders $atoms before cells, and $atoms in ascending LSB order.

Accepts

.a is a $noun.

.b is a $noun.

Produces

A $flag.

Source

++  aor
  ~/  %aor
  |=  [a=* b=*]
  ^-  ?
  ?:  =(a b)  &
  ?.  ?=(@ a)
    ?:  ?=(@ b)  |
    ?:  =(-.a -.b)
      $(a +.a, b +.b)
    $(a -.a, b -.b)
  ?.  ?=(@ b)  &
  |-
  =+  [c=(end 3 a) d=(end 3 b)]
  ?:  =(c d)
    $(a (rsh 3 a), b (rsh 3 b))
  (lth c d)

Examples

Note the possible differences with +dor due to comparing one byte at a time:

Discussion

This is different than +dor in that it compares $atoms one byte at a time, while +dor compares whole $atoms at once. Note that because it simply compares bytes, it doesn't account for multi-byte UTF-8 characters and the like.


+dor

Depth order.

Computes whether .a and .b are in ascending tree depth order, producing a $flag. Orders $atoms before cells, and $atoms in ascending numerical order.

Accepts

.a is a $noun.

.b is a $noun.

Produces

A $flag.

Source

Examples

Note the possible difference with +aor due to comparing whole $atoms rather than one byte at a time:

Discussion

If .a and .b are both $atoms, +dor is equivalent to +lte. If they're cells, +dor recurses on the heads, and then if the heads are the same it checks the tails.

If one sample is a cell and the other is an $atom, the cell sample is treated as "greater."


+gor

Mug order.

Computes whether of (mug a) and (mug b) are in ascending numeric order, producing a $flag. If the +mug hashes are equal, .a and .b are compared by +dor instead.

+mug is the the 31-bit nonzero FNV-1a hash algorithm.

Accepts

.a is a $noun.

.b is a $noun.

Produces

A $flag.

Source

Examples

Discussion

+maps use +gor on the key for horizontal ordering and +mor for vertical order. +maps only look at the keys (the head of the key-value pair elements) for ordering.


+mor

(more) mug order.

Computes whether the double-hashes (mug (mug a)) and (mug (mug b)) are in ascending numeric order, producing a $flag. If the double-+mug hashes are equal, .a and .b are compared by +dor instead.

+mug is the the 31-bit nonzero FNV-1a hash algorithm.

Accepts

.a is a $noun

.b is a $noun

Produces

A $flag.

Source

Examples

Discussion

Maps, sets, and queues all use +mor to check for vertical ordering. Maps and sets also use +gor for horizontal order, but queues use vertical ordering alone.

Since hashing removes correlation, double-+mugging with +mor removes correlation with single-+mugged +gor. Vertical order becomes uncorrelated with horizontal order.


Last updated