utils: Add hash-pred and use it in variable-mapping?
This commit is contained in:
parent
d94f72b1a5
commit
009c6109a9
1 changed files with 15 additions and 5 deletions
20
utils.rkt
20
utils.rkt
|
@ -12,7 +12,7 @@
|
|||
(contract-out [eval-with (-> variable-mapping? any/c any)]
|
||||
[extract-symbols (-> any/c list?)])
|
||||
;; Functions without contracts
|
||||
variable-mapping?
|
||||
variable-mapping? hash-pred
|
||||
;; Syntax
|
||||
auto-hash-ref/explicit auto-hash-ref/:)
|
||||
|
||||
|
@ -24,12 +24,22 @@
|
|||
;;; usage of hash tables mapping symbols to values. The goal is
|
||||
;;; essentially to avoid having to write explicit hash-ref calls.
|
||||
|
||||
;;; A variable mapping is a hash table mapping symbols to values.
|
||||
(define (variable-mapping? dict)
|
||||
;;; 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))
|
||||
(symbol? (car (hash-keys dict)))
|
||||
#t)))
|
||||
(if key-pred
|
||||
(key-pred (car (hash-keys dict)))
|
||||
#t)
|
||||
(if val-pred
|
||||
(val-pred (car (hash-values dict)))
|
||||
#t))))
|
||||
|
||||
;;; A variable mapping is a hash table mapping symbols to values.
|
||||
(define (variable-mapping? dict) (hash-pred dict #:key-pred symbol?))
|
||||
|
||||
;;; Given a (HashTable Symbol a) and a sequence of symbols, binds
|
||||
;;; these symbols to the values they are associated to in the hash
|
||||
|
|
Loading…
Reference in a new issue