kw-hash.scrbl (2660B)
1 #lang scribble/manual 2 @(require scribble/eval 3 (for-label kw-utils/kw-hash 4 kw-utils/kw-hash-lambda 5 kw-utils/kw-hash/contract 6 kw-utils/kw-lists-lambda 7 racket/base 8 racket/contract/base 9 racket/math 10 )) 11 12 @title[#:tag "kw-hash.scrbl"]{kw-hash} 13 14 @section{kw-hash-lambda} 15 16 @defmodule[kw-utils/kw-hash-lambda] 17 18 @defform[(kw-hash-lambda formals #:kws kw-hash-id body-expr ...+)]{ 19 roughly equivalent to 20 @(racketblock 21 (kw-lists-lambda kws kw-args formals 22 (let ([kw-hash-id (keyword-apply make-kw-hash kws kw-args '())]) 23 body ...))) 24 25 @examples[ 26 (require kw-utils/kw-hash-lambda) 27 (define proc 28 (kw-hash-lambda rest-args #:kws kw-hash 29 (list rest-args kw-hash))) 30 (proc 0 1 2 #:a 'a #:b 'b) 31 ]} 32 33 @defform[(kw-hash-case-lambda #:kws kw-hash-id [formals body-expr ...+] ...)]{ 34 roughly equivalent to 35 @(racketblock 36 (kw-lists-case-lambda kws kw-args 37 [formals 38 (let ([kw-hash-id (keyword-apply make-kw-hash kws kw-args '())]) 39 body ...)] 40 ...)) 41 42 @examples[ 43 (require kw-utils/kw-hash-lambda) 44 (define proc 45 (kw-hash-case-lambda #:kws kw-hash 46 [(a) 47 (list a kw-hash)] 48 [(a b) 49 (list a b kw-hash)])) 50 (proc 0 #:a 'a #:b 'b) 51 (proc 0 1 #:a 'a #:b 'b) 52 ]} 53 54 @section{kw-hash} 55 56 @defmodule[kw-utils/kw-hash] 57 58 @defproc[(apply/kw-hash [proc procedure?] [kw-hash (hash/c keyword? any/c)] [v any/c] ... [lst list?]) 59 any]{ 60 like @racket[keyword-apply], but instead of taking the keywords and keyword 61 arguments as separate lists, @racket[apply/kw-hash] takes them in a hash-table. 62 63 Based on @url["https://gist.github.com/Metaxal/578b473bc48886f81123"]. 64 65 @examples[ 66 (require kw-utils/kw-hash racket/math) 67 (define (kinetic-energy #:m m #:v v) 68 (* 1/2 m (sqr v))) 69 (apply/kw-hash kinetic-energy (hash '#:m 2 '#:v 1) '()) 70 ]} 71 72 @defproc[(app/kw-hash [proc procedure?] [kw-hash (hash/c keyword? any/c)] [v any/c] ...) 73 any]{ 74 like @racket[apply/kw-hash], but doesn't take a list argument at the end. 75 } 76 77 @defproc[(make-kw-hash [#:<kw> kw-arg any/c] ...) (hash/c keyword? any/c)]{ 78 returns a hash-table containing the given keyword arguments. 79 } 80 81 @section{kw-hash contracts} 82 83 @defmodule[kw-utils/kw-hash/contract] 84 85 @defform*[#:literals (any) 86 [(kw-hash-> [arg/c ...] #:kws kw-hash/c any) 87 (kw-hash-> [arg/c ...] #:rest rest/c #:kws kw-hash/c any)]]{ 88 Produces a contract for functions that can accept arbitrary keyword arguments. 89 The contract puts the keywords in a hash table and checks that against the 90 @racket[kw-hash/c] contract. 91 } 92