3232 'rebellion/type/tuple )
3333 #:private (list 'racket/base )))
3434
35+ @(define lexicographic-order-url "https://en.wikipedia.org/wiki/Lexicographic_order " )
36+
37+
3538@title{Comparators}
3639@defmodule[rebellion/base/comparator]
3740
@@ -199,6 +202,34 @@ with equality unless otherwise stated.
199202 (sorting (comparator-chain gemstone-by-type<=> gemstone-by-weight<=>))
200203 #:into into-list))}
201204
205+
206+ @defproc[(lexicographic-comparator [element-comparator comparator?])
207+ (comparator/c (sequence/c any/c))]{
208+ Constructs a @tech{comparator} of
209+ @tech[#:doc '(lib "scribblings/reference/reference.scrbl " )]{sequences} which compares sequences in
210+ @hyperlink[lexicographic-order-url]{lexicographic order} by comparing each sequence element with
211+ @racket[element-comparator].
212+
213+ @(examples
214+ #:eval (make-evaluator) #:once
215+ (eval:no-prompt
216+ (define real-seq<=> (lexicographic-comparator real<=>)))
217+ (compare real-seq<=> (list 1 2 3 ) (list 3 2 1 ))
218+ (compare real-seq<=> (list 1 2 3 ) (list 1 ))
219+ (compare real-seq<=> (list 1 2 3 ) (list 2 ))
220+ (compare real-seq<=> (list 1 2 3 ) (list 1 2 3 ))
221+ (compare real-seq<=> (list 1 2 3 ) (list 1 2 3 0 )))
222+
223+ The two sequences need not be of the same type: they will be considered equivalent if they have
224+ equivalent elements in the same order. This makes this comparator @tech{inconsistent with equality}
225+ when two sequences are not @racket[equal?] but contain equal elements in the same order.
226+
227+ @(examples
228+ #:eval (make-evaluator) #:once
229+ (equal? (list 1 2 3 ) (vector 1 2 3 ))
230+ (compare (lexicographic-comparator real<=>) (list 1 2 3 ) (vector 1 2 3 )))}
231+
232+
202233@section{Predefined Comparators}
203234
204235@defthing[real<=> (comparator/c comparable-real?)]{
@@ -248,8 +279,8 @@ with equality unless otherwise stated.
248279 (eval:error (compare natural<=> 42 -10 )))}
249280
250281@defthing[string<=> (comparator/c immutable-string?)]{
251- A @tech{comparator} that lexicographically compares immutable strings. Mutable
252- strings are disallowed, to prevent clients from concurrently mutating a string
282+ A @tech{comparator} that @hyperlink[lexicographic-order-url]{ lexicographically} compares immutable
283+ strings. Mutable strings are disallowed, to prevent clients from concurrently mutating a string
253284 while it's being compared.
254285
255286 @(examples
@@ -266,11 +297,11 @@ with equality unless otherwise stated.
266297 (compare char<=> #\a #\z ))}
267298
268299@defthing[symbol<=> (comparator/c symbol?)]{
269- A @tech{comparator} that lexicographically compares symbols. Symbols are equivalent if they contain
270- the same characters. Note that this comparator is @tech{inconsistent with equality}, because symbols
271- that print the same are not necessarily equal, due to the existence of unreadable and uninterned
272- symbols. If only interned symbols need to be compared, use @racket[interned-symbol<=>] to ensure
273- comparisons are consistent with equality.
300+ A @tech{comparator} that @hyperlink[lexicographic-order-url]{ lexicographically} compares symbols.
301+ Symbols are equivalent if they contain the same characters. Note that this comparator is
302+ @tech{inconsistent with equality}, because symbols that print the same are not necessarily equal, due
303+ to the existence of unreadable and uninterned symbols. If only interned symbols need to be compared,
304+ use @racket[interned-symbol<=>] to ensure comparisons are consistent with equality.
274305
275306 @(examples
276307 #:eval (make-evaluator) #:once
0 commit comments