In Racket, you can use a higher-order hash-map procedure, which usually returns a list with the values obtained as a result of applying the procedure obtained as a parameter, but can be adapted to change the map in place. For instance:
(define h (make-hash)) (hash-set! h "apple" 1) (hash-set! h "pear" 2) h => '#hash(("pear" . 2) ("apple" . 1))
The trick goes through lambda with the corresponding functionality:
(hash-map h (lambda (key value) (let ((newval (add1 value))) (hash-set! h key newval) newval))) h => '#hash(("pear" . 3) ("apple" . 2))
For a more general solution, try this implementation:
(define (mutable-hash-map! hash proc) (hash-map hash (lambda (key value) (hash-set! hash key (proc value)))) (void))
source share