Skip to content

Commit 000c75c

Browse files
authored
Improve hash-update refactoring (#421)
Closes #419.
1 parent 29fafb7 commit 000c75c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

default-recommendations/hash-shortcuts-test.rkt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ test: "hash-set! with hash-ref and literal keys can be simplified to hash-update
162162
------------------------------
163163

164164

165+
test: "hash-set! with hash-ref can be simplified to hash-update! without lambda"
166+
------------------------------
167+
(define h (make-hash))
168+
(define k 'a)
169+
(hash-set! h k (add1 (hash-ref h k 0)))
170+
------------------------------
171+
------------------------------
172+
(define h (make-hash))
173+
(define k 'a)
174+
(hash-update! h k add1 0)
175+
------------------------------
176+
177+
165178
test: "hash-set! with hash-ref cannot be simplified when v would shadow"
166179
------------------------------
167180
(define h (make-hash))

default-recommendations/hash-shortcuts.rkt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
(require (for-syntax racket/base)
13+
racket/list
1314
racket/set
1415
rebellion/private/static-name
1516
resyntax/base
@@ -107,9 +108,11 @@
107108
#:when (syntax-free-identifier=? #'k1 #'k2)
108109
#:when (for/and ([id (in-syntax-identifiers #'(f arg-before ... arg-after ...))])
109110
(not (equal? (syntax-e id) 'v)))
110-
(hash-update! h1 k1
111-
(λ (v) (f arg-before ... v arg-after ...))
112-
(~? failure-result)))
111+
#:with updater
112+
(if (and (empty? (attribute arg-before)) (empty? (attribute arg-after)))
113+
#'f
114+
#'(λ (v) (f arg-before ... v arg-after ...)))
115+
(hash-update! h1 k1 updater (~? failure-result)))
113116

114117

115118
(define-refactoring-rule hash-map-to-hash-keys

0 commit comments

Comments
 (0)