Is it ok to mutate a hash while iterating over it say in one of ~for~ forms?
Specifically I want to filter (that is drop) some keys, but I'm also interested in
mutation in general. I guess the answer lies in whether forms like ~in-dict~ etc
create lazy streams that hold on to the table?
Relevant docs that I managed to dig out:
-
hash-map seems to suggest that at least dropping keys is fine, but that only
talks about hash-map procedure specifically not other forms;
Here're some examples to be concrete:
#+begin_src racket
;; IMO ok according to docs?
(hash-map h (λ (k v) (when (pred v) (hash-remove! h k))))
;; probably ok assuming it translates to hash-map?
(dict-map h (λ (k v) (when (pred v) (dict-remove! h k))))
;; is that ok?
(for (((k v) (in-dict h))
#:when (pred v))
(dict-remove! h k))
;; defensive solution
(let ((fails (for/list (((k v) (in-dict h))
#:when (pred v))
k)))
(for-each (curry dict-remove! h) fails))
#+end_src
That question wouldn't arise were I to deal with immutable data, but I'm not.