From 9eca7bf4490766426c2a7524b03f2d7337ca49e9 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 23 Feb 2020 08:57:50 +0100 Subject: [PATCH] Prefer for, for/list, and for/hash for iterating over hashes. --- networks.rkt | 10 ++++------ utils.rkt | 6 ++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/networks.rkt b/networks.rkt index 2edb27b..01bee69 100644 --- a/networks.rkt +++ b/networks.rkt @@ -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) diff --git a/utils.rkt b/utils.rkt index deaa3a3..7dc9ca9 100644 --- a/utils.rkt +++ b/utils.rkt @@ -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))