on 883cb21 help sign in
315 lines 4.7 kB history raw
1+++
2title = "2f: Noun Ordering"
3weight = 9
4template = "doc.html"
5+++
7## `++aor`
9Alphabetical order
11Computes whether `a` and `b` are in alphabetical order, producing a flag. Orders atoms before cells, and atoms in ascending LSB order.
13#### Accepts
15`a` is a noun.
17`b` is a noun.
19#### Produces
21A flag.
23#### Source
25```hoon
26++ aor
27 ~/ %aor
28 |= [a=* b=*]
29 ^- ?
30 ?: =(a b) &
31 ?. ?=(@ a)
32 ?: ?=(@ b) |
33 ?: =(-.a -.b)
34 $(a +.a, b +.b)
35 $(a -.a, b -.b)
36 ?. ?=(@ b) &
37 |-
38 =+ [c=(end 3 a) d=(end 3 b)]
39 ?: =(c d)
40 $(a (rsh 3 a), b (rsh 3 b))
41 (lth c d)
42```
44#### Examples
46```
47> (aor 'a' 'b')
48%.y
49> (aor 'b' 'a')
50%.n
51> (aor 'a' 'a')
52%.y
54> (aor 1 2)
55%.y
56> (aor 2 1)
57%.n
59> (aor ['a' ~] 'b')
60%.n
61> (aor 'b' ['a' ~])
62%.y
64> (aor ['a' ~] ['b' ~])
65%.y
66> (aor ['b' ~] ['a' ~])
67%.n
69> (aor "abca" "abcz")
70%.y
71> (aor "abcz" "abca")
72%.n
74> (aor 0b1011 0b1010)
75%.n
76> (aor 0b1010 0b1011)
77%.y
79> (aor [1 2] [2 1])
80%.y
81> (aor [2 1] [1 2])
82%.n
83```
85Note the possible differences with `+dor` due to comparing one byte at a time:
87```
88> (aor 0b1001.0000.0000 0b1000.1000.0000)
89%.y
90> (dor 0b1001.0000.0000 0b1000.1000.0000)
91%.n
92```
94#### Discussion
96This is different than [+dor](#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.
98---
100## `++dor`
102Depth order
104Computes whether `a` and `b` are in ascending tree depth order, producing a flag. Orders atoms before cells, and atoms in ascending numerical order.
106#### Accepts
108`a` is a noun.
110`b` is a noun.
112#### Produces
114A flag.
116#### Source
118```hoon
119++ dor
120 ~/ %dor
121 |= [a=* b=*]
122 ^- ?
123 ?: =(a b) &
124 ?. ?=(@ a)
125 ?: ?=(@ b) |
126 ?: =(-.a -.b)
127 $(a +.a, b +.b)
128 $(a -.a, b -.b)
129 ?. ?=(@ b) &
130 (lth a b)
131```
133#### Examples
135```
136> (dor 1 2)
137%.y
139> (dor 2 1)
140%.n
142> (dor ~[1 2 3] ~[1 2 4])
143%.y
145> (dor ~[1 2 4] ~[1 2 3])
146%.n
148> (dor `(list @)`~[99 100 10.000] ~[99 101 10.000])
149%.y
151> (dor ~[99 101 10.999] `(list @)`~[99 100 10.000])
152%.n
153```
155Note the possible difference with [+aor](#aor) due to comparing whole atoms rather than one byte at a time:
157```
158> (aor 0b1001.0000.0000 0b1000.1000.0000)
159%.y
160> (dor 0b1001.0000.0000 0b1000.1000.0000)
161%.n
162```
164#### Discussion
166If `a` and `b` are both atoms, `dor` is equivalent to [`lte`](/docs/hoon/reference/stdlib/1a#lte). If they're
167cells, `dor` recurses on the heads, and then if the heads are the same it checks
168the tails.
170If one sample is a cell and the other is an atom, the cell sample is treated as
171"greater."
173---
175## `++gor`
177Mug order
179Computes whether of `(mug a)` and `(mug b)` are in ascending numeric order,
180producing a flag. If the `mug` hashes are equal, `a` and `b` are compared by
181`dor` instead.
183[`mug`](/docs/hoon/reference/stdlib/2e#mug) is the the 31-bit nonzero FNV-1a hash algorithm.
185#### Accepts
187`a` is a noun.
189`b` is a noun.
191#### Produces
193A flag.
195#### Source
197```hoon
198++ gor
199 ~/ %gor
200 |= [a=* b=*]
201 ^- ?
202 =+ [c=(mug a) d=(mug b)]
203 ?: =(c d)
204 (dor a b)
205 (lth c d)
206```
208#### Examples
210```
211> (gor 'd' 'c')
212%.y
214> 'd'
215'd'
216> 'c'
217'c'
219> `@ud`'d'
220100
221> `@ud`'c'
22299
224> (mug 'd')
2251.628.185.714
226> (mug 'c')
2271.712.073.811
229> (gor 'd' 'c')
230%.y
231> (gor 'c' 'd')
232%.n
233```
235```
236> (gor "foo" "bar")
237%.n
238```
240```
241> (gor (some 10) `(list @)`[1 2 3 ~])
242%.n
243```
245#### Discussion
247`map`s use `gor` on the key for horizontal ordering and `mor` for
248vertical order. `map`s only look at the keys (the head of the key-value pair
249elements) for ordering.
251---
253## `++mor`
255(more) mug order
257Computes whether the double-hashes `(mug (mug a))` and `(mug (mug b))` are in
258ascending numeric order, producing a flag. If the double-`mug` hashes are
259equal, `a` and `b` are compared by `dor` instead.
261[`mug`](/docs/hoon/reference/stdlib/2e#mug) is the the 31-bit nonzero FNV-1a hash algorithm.
263#### Accepts
265`a` is a noun
267`b` is a noun
269#### Produces
271A flag.
273#### Source
275```hoon
276++ mor
277 ~/ %mor
278 |= [a=* b=*]
279 ^- ?
280 =+ [c=(mug (mug a)) d=(mug (mug b))]
281 ?: =(c d)
282 (dor a b)
283 (lth c d)
284```
286#### Examples
288```
289 > (mor 'f' 'g')
290 %.y
292 > [(mug 'f') (mug 'g')]
293 [1.661.740.952 1.644.963.335]
295 > [(mug (mug 'f')) (mug (mug 'g'))]
296 [261.421.509 1.861.258.547]
298 > (mor 'a' 'z')
299 %.n
301 > (mor 43.326 41.106)
302 %.n
303```
305#### Discussion
307Maps, sets, and queues all use `mor` to check for vertical ordering. Maps and
308sets also use `gor` for horizontal order, but queues use
309vertical ordering alone.
311Since hashing removes correlation, double-`mug`ging with `mor` removes
312correlation with single-`mug`ged `gor`. Vertical order becomes uncorrelated
313with horizontal order.
315---