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

View file

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