1 hash-lambda
(require hash-lambda) | package: hash-lambda |
> (define my+ (hash-lambda/match [(hash-table [0 a] [1 b]) (+ a b)])) > (my+ 1 2) 3
> (define/contract kinetic-energy (#:mass number? #:velocity number? . -> . number?) (hash-lambda/match [(hash-table ['#:mass mass] ['#:velocity velocity]) (* 1/2 mass (sqr velocity))])) > (kinetic-energy #:mass 2 #:velocity 1) 1
1.1 hash-lambda
syntax
(hash-lambda args-hash-id body ...+)
(hash-lambda [args-hash-id args-hash-contract] body ...+)
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.
> (define return-args-hash (hash-lambda args-hash args-hash)) > (return-args-hash "0" "1" #:keyword "keyword-argument" "2") '#hash((1 . "1") (#:keyword . "keyword-argument") (0 . "0") (2 . "2"))
> (define my+ (hash-lambda args-hash (+ (hash-ref args-hash 0) (hash-ref args-hash 1)))) > (my+ 1 2) 3
> (define/contract kinetic-energy (#:mass number? #:velocity number? . -> . number?) (hash-lambda args-hash (let ([mass (hash-ref args-hash '#:mass)] [velocity (hash-ref args-hash '#:velocity)]) (* 1/2 mass (sqr velocity))))) > (kinetic-energy #:mass 2 #:velocity 1) 1
syntax
(hash-lambda/match match-clause ...)
match-clause = [pat body ...+] | [pat (=> id) body ...+] | [pat #:when cond-expr body ...+]
(hash-lambda args-hash (match args-hash match-clause ...))
> (define my+ (hash-lambda/match [(hash-table [0 a] [1 b]) (+ a b)])) > (my+ 1 2) 3
> (define/contract kinetic-energy (#:mass number? #:velocity number? . -> . number?) (hash-lambda/match [(hash-table ['#:mass mass] ['#:velocity velocity]) (* 1/2 mass (sqr velocity))])) > (kinetic-energy #:mass 2 #:velocity 1) 1
1.2 args-hashes and apply/hash
procedure
(apply/hash proc args-hash #:<kw> kw-arg ...) → any
proc : procedure? args-hash : args-hash? kw-arg : any/c
> (apply/hash list (hash 0 "0" 1 "1")) '("0" "1")
> (apply/hash list (hash 1 "1" 0 "0")) '("0" "1")
> (define (kinetic-energy #:mass m #:velocity v) (* 1/2 m (sqr v))) > (apply/hash kinetic-energy (hash '#:mass 2 '#:velocity 1)) 1
procedure
(args-hash? x) → boolean?
x : any/c
procedure
(make-args-hash stuff ... #:<kw> more-stuff ...) → args-hash? stuff : any/c more-stuff : any/c
> (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.
> (match (make-args-hash 1 2 3 #:kw-1 'kw-arg-1 #:kw-2 'kw-arg-2) [(make-args-hash one two three #:kw-1 kw-arg-one #:kw-2 kw-arg-two) (list one two three kw-arg-one kw-arg-two)]) '(1 2 3 kw-arg-1 kw-arg-2)
procedure
(args-hash-first args-hash) → any/c
args-hash : (and/c args-hash? (hash-has-key?/c 0))
procedure
(args-hash-rest args-hash) → args-hash?
args-hash : (and/c args-hash? (hash-has-key?/c 0))
> (args-hash-rest (hash 0 "0" 1 "1" 2 "2" '#:kw "kw-arg")) '#hash((1 . "2") (#:kw . "kw-arg") (0 . "1"))
procedure
(args-hash-cons val args-hash) → args-hash?
val : any/c args-hash : args-hash?
> (args-hash-cons "thing" (hash 0 "0" 1 "1" 2 "2" '#:kw "kw-arg")) '#hash((1 . "0") (3 . "2") (#:kw . "kw-arg") (0 . "thing") (2 . "1"))
args-hash-cons is also bound as a match expander to be used with match.
> (match (args-hash-cons "thing" (hash 0 "0" 1 "1" 2 "2" '#:kw "kw-arg")) [(args-hash-cons val hash) (cons val hash)]) '("thing" . #hash((1 . "1") (#:kw . "kw-arg") (0 . "0") (2 . "2")))
procedure
(args-hash-cons* val ... args-hash) → args-hash?
val : any/c args-hash : args-hash?
> (args-hash-cons* "thing" "other-thing" (hash 0 "0" 1 "1" 2 "2" '#:kw "kw-arg")) '#hash((1 . "other-thing") (3 . "1") (#:kw . "kw-arg") (0 . "thing") (2 . "0") (4 . "2"))
args-hash-cons* is also bound as a match expander to be used with match.
> (match (args-hash-cons* "thing" "other-thing" (hash 0 "0" 1 "1" 2 "2" '#:kw "kw-arg")) [(args-hash-cons* val other-val hash) (list val other-val hash)]) '("thing" "other-thing" #hash((1 . "1") (#:kw . "kw-arg") (0 . "0") (2 . "2")))
procedure
(args-hash-append args-hash ...) → args-hash?
args-hash : args-hash?
> (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") (#:kw . "kw-arg") (#:other-kw . "other-kw-arg") (0 . "0") (2 . "2") (4 . "other-1"))
procedure
(args-hash->args-list args-hash) → (or/c list? #f)
args-hash : any/c
1.3 misc.
procedure
(hash-has-key?/c key) → flat-contract?
key : any/c
> (define return-first-arg (hash-lambda [args-hash (hash-has-key?/c 0)] (hash-ref args-hash 0))) > (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)
> (define return-first-arg (hash-lambda [args-hash (match?/c (hash-table [0 0-val] [keys vals] ...))] (hash-ref args-hash 0))) > (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)
procedure
(make-hash-lambda-contract args-hash-contract [ range-contract]) → contract? args-hash-contract : (or/c contract? 'any) range-contract : (or/c contract? 'any) = 'any
> (define/contract kinetic-energy (make-hash-lambda-contract (match?/c (hash-table ['#:mass (? number? m)] ['#:velocity (? number? v)])) number?) (hash-lambda/match [(hash-table ['#:mass (? number? m)] ['#:velocity (? number? v)]) (* 1/2 m (sqr v))])) > (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