Skip to content

Commit

Permalink
Add some hash-map shortcuts (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfirth authored Aug 31, 2024
1 parent fd890a1 commit 9738e69
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
22 changes: 22 additions & 0 deletions default-recommendations/hash-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,25 @@ test: "hash-set! with hash-ref cannot be simplified when v would shadow"
(define v 5)
(hash-set! h k (+ v (hash-ref h k 0)))
------------------------------


test: "hash-map with key-returning lamda can be refactored to hash-keys"
------------------------------
(define h (make-hash))
(hash-map h (λ (k v) k))
------------------------------
------------------------------
(define h (make-hash))
(hash-keys h)
------------------------------


test: "hash-map with value-returning lamda can be refactored to hash-values"
------------------------------
(define h (make-hash))
(hash-map h (λ (k v) v))
------------------------------
------------------------------
(define h (make-hash))
(hash-values h)
------------------------------
20 changes: 19 additions & 1 deletion default-recommendations/hash-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,28 @@
(~? failure-result))])


(define-refactoring-rule hash-map-to-hash-keys
#:description "This `hash-map` expression is equivalent to the `hash-keys` function."
#:literals (hash-map)
[(hash-map h (_:lambda-by-any-name (k1:id v) k2:id))
#:when (free-identifier=? #'k1 #'k2)
(hash-keys h)])


(define-refactoring-rule hash-map-to-hash-values
#:description "This `hash-map` expression is equivalent to the `hash-values` function."
#:literals (hash-map)
[(hash-map h (_:lambda-by-any-name (k v1:id) v2:id))
#:when (free-identifier=? #'v1 #'v2)
(hash-values h)])


(define hash-shortcuts
(refactoring-suite
#:name (name hash-shortcuts)
#:rules (list hash-ref-set!-to-hash-ref!
#:rules (list hash-map-to-hash-keys
hash-map-to-hash-values
hash-ref-set!-to-hash-ref!
hash-ref-set!-with-constant-to-hash-ref!
hash-ref-with-constant-lambda-to-hash-ref-without-lambda
hash-ref!-with-constant-lambda-to-hash-ref!-without-lambda
Expand Down

0 comments on commit 9738e69

Please sign in to comment.