Prefer for, for/list, and for/hash for iterating over hashes.

This commit is contained in:
Sergiu Ivanov 2020-02-23 08:57:50 +01:00
parent 5fd1b95d5c
commit 9eca7bf449
2 changed files with 6 additions and 10 deletions

View File

@ -102,9 +102,8 @@
;;; Build a network from a network form.
(define (network-form->network bnf)
(make-immutable-hash
(hash-map bnf (λ (x form)
(cons x (update-function-form->update-function form))))))
(for/hash ([(x form) bnf])
(values x (update-function-form->update-function form))))
;;; Build a network from a list of pairs of forms of update functions.
(define (make-network-from-forms forms)
@ -145,8 +144,7 @@
(define (build-interaction-graph n)
(transpose
(unweighted-graph/adj
(hash-map n (λ (var _)
(cons var (list-interactions n var)))))))
(for/list ([(var _) n]) (cons var (list-interactions n var))))))
;;; Given a list of pairs mapping variables to generic sets of their
;;; possible values, constructs the list of all possible states.
@ -164,7 +162,7 @@
;;; Makes a hash set mapping all variables to a single domain.
(define (make-same-domains vars domain)
(make-immutable-hash (for/list ([var vars]) (cons var domain))))
(for/hash ([var vars]) (values var domain)))
;;; Makes a hash set mapping all variables to the Boolean domain.
(define (make-boolean-domains vars)

View File

@ -117,8 +117,7 @@
;;;
(define (eval-with ht expr)
(parameterize ([current-namespace (make-base-namespace)])
(hash-for-each ht (λ (x val)
(namespace-set-variable-value! x val)))
(for ([(x val) ht]) (namespace-set-variable-value! x val))
(eval expr)))
;;; Same as eval-with, but returns only the first value produced by
@ -166,8 +165,7 @@
;;; Converts all the values of a variable mapping to string.
(define (stringify-variable-mapping ht)
(make-hash (hash-map ht (λ (key val)
(cons key (any->string val))))))
(for/hash ([(key val) ht]) (values key (any->string val))))
;;; A shortcut for variable-mapping-stingify.
(define-syntax-rule (sgfy ht) (stringify-variable-mapping ht))