Skip to content

Commit

Permalink
Improve hash-update refactoring (#421)
Browse files Browse the repository at this point in the history
Closes #419.
  • Loading branch information
jackfirth authored Nov 24, 2024
1 parent 29fafb7 commit 000c75c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 13 additions & 0 deletions default-recommendations/hash-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ test: "hash-set! with hash-ref and literal keys can be simplified to hash-update
------------------------------


test: "hash-set! with hash-ref can be simplified to hash-update! without lambda"
------------------------------
(define h (make-hash))
(define k 'a)
(hash-set! h k (add1 (hash-ref h k 0)))
------------------------------
------------------------------
(define h (make-hash))
(define k 'a)
(hash-update! h k add1 0)
------------------------------


test: "hash-set! with hash-ref cannot be simplified when v would shadow"
------------------------------
(define h (make-hash))
Expand Down
9 changes: 6 additions & 3 deletions default-recommendations/hash-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


(require (for-syntax racket/base)
racket/list
racket/set
rebellion/private/static-name
resyntax/base
Expand Down Expand Up @@ -107,9 +108,11 @@
#:when (syntax-free-identifier=? #'k1 #'k2)
#:when (for/and ([id (in-syntax-identifiers #'(f arg-before ... arg-after ...))])
(not (equal? (syntax-e id) 'v)))
(hash-update! h1 k1
(λ (v) (f arg-before ... v arg-after ...))
(~? failure-result)))
#:with updater
(if (and (empty? (attribute arg-before)) (empty? (attribute arg-after)))
#'f
#'(λ (v) (f arg-before ... v arg-after ...)))
(hash-update! h1 k1 updater (~? failure-result)))


(define-refactoring-rule hash-map-to-hash-keys
Expand Down

0 comments on commit 000c75c

Please sign in to comment.