3 Orders and Ordered Dictionaries
(require data/order) |
This library defines orders and the ordered dictionary generic interface.
|
e/c = (->i ([d ordered-dict?]) |
[_ (d) (or/c #f (dict-iter-contract d))]) |
s/c = (->i ([d ordered-dict?] |
[k (d) (dict-key-contract d)]) |
[_ (d) (or/c #f (dict-iter-contract d))]) |
The procedures are implementations of the following generic functions:
A struct type that implements prop:ordered-dict must also implement prop:dict.
(ordered-dict? x) → boolean? |
x : any/c |
| ||
|
| |||
| |||
| |||
|
(order name domain-contract comparator) |
→ (and/c order? procedure?) |
name : symbol? |
domain-contract : contract? |
comparator : (-> any/c any/c ordering/c) |
(order name domain-contract =? <? [>?]) → (and/c order? procedure?) |
name : symbol? |
domain-contract : contract? |
=? : (-> any/c any/c boolean?) |
<? : (-> any/c any/c boolean?) |
>? : (-> any/c any/c boolean?) = (lambda (x y) (<? y x)) |
The domain-contract is not applied to the comparison function; rather, clients of the order are advised to incorporate the domain contracts into their own contracts. For example, when a splay-tree (see data/splay-tree) is constructed with an order, it applies the domain-contract to its keys. Thus the contract is checked once per dictionary procedure call, rather than on every comparison.
An order object is applicable as a procedure; it behaves as its comparator.
Examples: |
> (define string-order (order 'string-order string? string=? string<?)) |
> (string-order "abc" "acdc") |
'< |
> (string-order "x" 12) |
string=?: expects type <string> as 2nd argument, given: 12; |
other arguments were: "x" |
(order-comparator ord) → (-> any/c any/c ordering/c) |
ord : order? |
(order-domain-contract ord) → contract? |
ord : order? |
| ||
|
Examples: |
> (real-order 1.0 1) |
'= |
> (real-order 5 7) |
'< |
> (real-order 9.0 3.4) |
'> |
> (real-order 1 +inf.0) |
'< |
> (real-order 5 -inf.0) |
'> |
The order is designed so that lists, vectors, and prefab structs are ordered lexicographically.
Warning! The datum-order is not compatible with the standard numeric order; all exact numbers are ordered before all inexact numbers. This allows 1 to be considered distinct from 1.0, for example.
The following built-in data types are currently supported: numbers, strings, bytes, keywords, symbols, booleans, characters, null, pairs, vectors, boxes, and prefab structs.
Examples: |
> (datum-order 1 2) |
'< |
> (datum-order 8 5.0) |
'< |
> (datum-order 3+5i 3+2i) |
'> |
> (datum-order '(a #:b c) '(a #:c d c)) |
'< |
> (datum-order "apricot" "apple") |
'> |
> (datum-order '#(1 2 3) '#(1 2)) |
'> |
> (datum-order '#(1 2 3) '#(1 3)) |
'< |
> (datum-order 'apple (box "candy")) |
'< |