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)]
|
(contract-out [eval-with (-> variable-mapping? any/c any)]
|
||||||
[extract-symbols (-> any/c list?)])
|
[extract-symbols (-> any/c list?)])
|
||||||
;; Functions without contracts
|
;; Functions without contracts
|
||||||
variable-mapping?
|
variable-mapping? hash-pred
|
||||||
;; Syntax
|
;; Syntax
|
||||||
auto-hash-ref/explicit auto-hash-ref/:)
|
auto-hash-ref/explicit auto-hash-ref/:)
|
||||||
|
|
||||||
|
@ -24,12 +24,22 @@
|
||||||
;;; usage of hash tables mapping symbols to values. The goal is
|
;;; usage of hash tables mapping symbols to values. The goal is
|
||||||
;;; essentially to avoid having to write explicit hash-ref calls.
|
;;; essentially to avoid having to write explicit hash-ref calls.
|
||||||
|
|
||||||
;;; A variable mapping is a hash table mapping symbols to values.
|
;;; Checks whether dict is a hash table and whether its keys satisfy
|
||||||
(define (variable-mapping? dict)
|
;;; 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)
|
(and (hash? dict)
|
||||||
(if (not (hash-empty? dict))
|
(if (not (hash-empty? dict))
|
||||||
(symbol? (car (hash-keys dict)))
|
(if key-pred
|
||||||
#t)))
|
(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
|
;;; Given a (HashTable Symbol a) and a sequence of symbols, binds
|
||||||
;;; these symbols to the values they are associated to in the hash
|
;;; these symbols to the values they are associated to in the hash
|
||||||
|
|
Loading…
Reference in a new issue