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.
;;; 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?))