From 009c6109a909bcb64224bffd0eb8aa5fe3afabec Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 20 Feb 2020 00:21:10 +0100 Subject: [PATCH] utils: Add hash-pred and use it in variable-mapping? --- utils.rkt | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/utils.rkt b/utils.rkt index debc252..7e0259e 100644 --- a/utils.rkt +++ b/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