1.1 Comparison Predicate Generation🔗ℹ
See also typed/alexis/util/comparator for Typed Racket-compatible forms.
|
|
maybe-adapter | | = | | | | | | | | #:adapter adapter-expr |
|
This provides a convenient macro for generating comparison predicates based on a "comparator"
function. The provided comparator-expr must evaluate to a function which takes two values and
produces either 0, 1, or -1. These values correspond to both parameters
being equal, the first parameter being greater, or the second parameter being greater, respectively.
Using this function, a set of functions is generated using predicate-base-id as a base
identifer to determine the names of the resulting definitions. This macro produces six functions,
their names acquired by appending =?, >?, <?, <>?, >=?,
and <=? to predicate-base-id.
If adapter-expr is provided, then it must evaluate to a function that takes a single
parameter and returns a single value. If specified, values will be threaded through
adapter-expr before being passed to comparator-expr. This allows values to
be mapped to other values before being provided to the comparator for additional processing or
parsing.
Examples:
> (struct num-str (num str)) |
> (define (num-str-compare a b) | (if (= (num-str-num a) (num-str-num b)) | (let ([a (num-str-str a)] | [b (num-str-str b)]) | (cond | [(string>? a b) 1] | [(string<? a b) -1] | [else 0])) | (let ([a (num-str-num a)] | [b (num-str-num b)]) | (cond | [(> a b) 1] | [(< a b) -1] | [else 0])))) |
|
|
> (num-str=? '(123 . "abc") '(123 . "abc")) |
#t |
> (num-str<? '(200 . "aaa") '(100 . "aaa")) |
#f |
1.3 Wrapping keyword procedures🔗ℹ
It’s easy to "wrap" existing procedures in order to extend them with additional functionality. For
example, the following wraps + to additionally call add1 on the result:
This is much harder to do, however, if the procedure being wrapped can accept keyword arguments, since
they are not accepted in the "rest argument" syntax. Instead, make-keyword-procedure and
keyword-apply must be used. This module provides some macros to make wrapping these
procedures easier.
Creates a new procedure that wraps original-proc-expr. Each application
of the resulting procedure calls original-proc-expr with the provided
arguments and binds result-id to the result, which is available to the
body-exprs. The return value of the resulting procedure is the result of
the final body-expr.
Examples:
|
> (check-duplicates+add1 #:key positive? '(1 1)) |
2 |
(define/wrapped name-id [result-id original-proc-expr] | body-expr ...+) |
|
|
|