hash-pred: Generalise and simplify.

This commit is contained in:
Sergiu Ivanov 2020-02-20 14:47:06 +01:00
parent 6001762cb8
commit 71f979808c

View file

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