diff --git a/utils.rkt b/utils.rkt index 8001dcc..18787db 100644 --- a/utils.rkt +++ b/utils.rkt @@ -27,18 +27,15 @@ ;;; essentially to avoid having to write explicit hash-ref calls. ;;; Checks whether dict is a hash table and whether its keys satisfy -;;; key-pred and its values satisfy val-pred. If one of the -;;; predicates is #f, it is not checked. For example, (hash-pred -;;; dict) is equivalent to (hash dict). -(define (hash-pred dict #:key-pred [key-pred #f] #:val-pred [val-pred #f]) - (and (hash? dict) - (if (not (hash-empty? dict)) - (if key-pred - (key-pred (car (hash-keys dict))) - #t) - (if val-pred - (val-pred (car (hash-values dict))) - #t)))) +;;; key-pred and its values satisfy val-pred. If either of the +;;; predicates is not supplied, it defaults to true. For example, +;;; (hash-pred dict) is equivalent to (hash dict). +(define/match (hash-pred dict + #:key-pred [key-pred (λ (_) #t)] + #:val-pred [val-pred (λ (_) #t)]) + [((hash-table (keys vals) ...) key-pred val-pred) + (and (andmap key-pred keys) (andmap val-pred vals))] + [(_ _ _) #f]) ;;; A variable mapping is a hash table mapping symbols to values. (define (variable-mapping? dict) (hash-pred dict #:key-pred symbol?))