6.2.901.900
1 hash-lambda
These forms and functions allow you to create a "rest argument" that includes keyword arguments.
see Declaring a Rest ArgumentInstead of storing the arguments in a list,
hash-lambda stores them in a hash table,
where you can use
(hash-ref args-hash 0), etc. to access by-position arguments,
and
(hash-ref args-hash '#:<kw>), etc. to access keyword arguments, or you can use
hash-lambda/match and use
match patterns for the arguments or use
apply/hash to apply an args-hash to a function.
Examples: |
| > (my+ 1 2) | 3 | | > (kinetic-energy #:mass 2 #:velocity 1) | 1 |
|
1.1 hash-lambda
like
(lambda args-list-id body ...+), except that it takes all keywords, and it puts its
arguments into a hash table instead of a list.
In the hash table, the by-position arguments have their position as the key, and the keyword arguments
have the (quoted) keyword as the key.
The second form is like the first form except that it applies args-hash-contract to
args-hash-id.
Examples: |
| > (return-args-hash "0" "1" #:keyword "keyword-argument" "2") | '#hash((1 . "1") (#:keyword . "keyword-argument") (0 . "0") (2 . "2")) | | > (my+ 1 2) | 3 | | > (kinetic-energy #:mass 2 #:velocity 1) | 1 |
|
(hash-lambda/match match-clause ...)
|
|
match-clause | | = | | [pat body ...+] | | | | | | [pat (=> id) body ...+] | | | | | | [pat #:when cond-expr body ...+] |
|
allows you to use pattern matching instead of putting lots of
hash-refs everywhere, and
allows the procedure to do different things based on which of the
pats match,
whether the the
#:when test passes, and whether
(=> id) or
(failure-cont)
is used.
Equivalent to:
Examples: |
| > (my+ 1 2) | 3 | | > (kinetic-energy #:mass 2 #:velocity 1) | 1 |
|
1.2 args-hashes and apply/hash
like
apply, except that it takes an
args-hash? instead of a
list,
and it doesn’t take by-position arguments before the last argument like
apply does.
determines whether
x is a valid args-hash for use in
apply/hashExample: |
> (make-args-hash 1 2 3 #:kw-1 'kw-arg-1 #:kw-2 'kw-arg-2) | '#hash((1 . 2) (#:kw-1 . kw-arg-1) (#:kw-2 . kw-arg-2) (0 . 1) (2 . 3)) |
|
make-args-hash is also bound as a match expander to be used with match.
Example: |
| '(1 2 3 kw-arg-1 kw-arg-2) |
|
returns an args-hash without the
0 key (with
(hash-remove args-hash 0)) and with all
of the number-keys reduced by one.
Example: |
> (args-hash-rest (hash 0 "0" 1 "1" 2 "2" '#:kw "kw-arg")) | '#hash((1 . "2") (0 . "1") (#:kw . "kw-arg")) |
|
returns args-hash with the number keys increased by one and with val added with a
key of 0.
Example: |
> (args-hash-cons "thing" (hash 0 "0" 1 "1" 2 "2" '#:kw "kw-arg")) | '#hash((1 . "0") (3 . "2") (0 . "thing") (2 . "1") (#:kw . "kw-arg")) |
|
args-hash-cons is also bound as a match expander to be used with match.
Example: |
| '("thing" . #hash((1 . "1") (0 . "0") (2 . "2") (#:kw . "kw-arg"))) |
|
Example: |
> (args-hash-cons* "thing" "other-thing" (hash 0 "0" 1 "1" 2 "2" '#:kw "kw-arg")) | '#hash((1 . "other-thing") (3 . "1") (0 . "thing") (2 . "0") (#:kw . "kw-arg") (4 . "2")) |
|
args-hash-cons* is also bound as a match expander to be used with match.
Example: |
| '("thing" "other-thing" #hash((1 . "1") (0 . "0") (2 . "2") (#:kw . "kw-arg"))) |
|
appends the args-hashes together, with the number keys of the later args-hashes
increased.
Example: |
> (args-hash-append (hash 0 "0" 1 "1" 2 "2" '#:kw "kw-arg") | (hash 0 "other-0" 1 "other-1" 2 "other-2" '#:other-kw "other-kw-arg")) |
| '#hash((1 . "1") (3 . "other-0") (5 . "other-2") (0 . "0") (2 . "2") (#:other-kw . "other-kw-arg") (#:kw . "kw-arg") (4 . "other-1")) |
|
equivalent to
(apply/hash list args-hash), except that if
args-hash either isn’t an
args-hash or contains any keywords, then it returns
#false instead of raising an exeption.
1.3 misc.
makes a contract that accepts hash tables that have the given key
Examples: |
| > (return-first-arg "first arg" "other-arg" #:kw "other-arg") | "first arg" | > (return-first-arg) | return-first-arg: contract violation | expected: (hash-has-key?/c (quote 0)) | given: '#hash() | in: the args-hash of | (make-hash-lambda-contract | (hash-has-key?/c '0) | any) | contract from: (definition return-first-arg) | blaming: top-level | (assuming the contract is correct) |
|
creates a flat-contract that accepts values that
match the given pattern.
Examples: |
| > (return-first-arg "first arg" "other-arg" #:kw "other-arg") | "first arg" | > (return-first-arg) | return-first-arg: contract violation | expected: (match?/c (hash-table (0 0-val) (keys vals) | ...)) | given: '#hash() | in: the args-hash of | (make-hash-lambda-contract | (match?/c | (hash-table (0 0-val) (keys vals) ...)) | any) | contract from: (definition return-first-arg) | blaming: top-level | (assuming the contract is correct) |
|
creats a contract for a function that takes an args-hash that matches args-hash-contract,
and produces something that matches range-contract.
Examples: |
| > (kinetic-energy #:mass 2 #:velocity 1) | 1 | > (kinetic-energy) | kinetic-energy: contract violation | expected: (match?/c (hash-table ((quote #:mass) (? number? | m)) ((quote #:velocity) (? number? v)))) | given: '#hash() | in: the args-hash of | (make-hash-lambda-contract | (match?/c | (hash-table | ('#:mass (? number? m)) | ('#:velocity (? number? v)))) | number?) | contract from: (definition kinetic-energy) | blaming: top-level | (assuming the contract is correct) | at: eval:1.0 |
|