Skip to content

Commit

Permalink
Expand numeric comparison shortcuts (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfirth authored Nov 22, 2024
1 parent d32b0d1 commit 696e9ba
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
20 changes: 20 additions & 0 deletions default-recommendations/numeric-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,35 @@ test: "lambda equivalent to sub1 refactorable to sub1"
- (map sub1 (list 1 2 3))


test: "comparison equivalent to positive? refactorable to positive?"
- (> 42 0)
- (< 0 42)
- (not (<= 42 0))
- (not (>= 0 42))
- (positive? 42)


test: "comparison equivalent to negative? refactorable to negative?"
- (< 42 0)
- (> 0 42)
- (not (>= 42 0))
- (not (<= 0 42))
- (negative? 42)


test: "lambda equivalent to positive? refactorable to positive?"
- (filter (λ (x) (> x 0)) (list -2 -1 0 1 2))
- (filter (λ (x) (< 0 x)) (list -2 -1 0 1 2))
- (filter (λ (x) (not (<= x 0))) (list -2 -1 0 1 2))
- (filter (λ (x) (not (>= 0 x))) (list -2 -1 0 1 2))
- (filter positive? (list -2 -1 0 1 2))


test: "lambda equivalent to negative? refactorable to negative?"
- (filter (λ (x) (< x 0)) (list -2 -1 0 1 2))
- (filter (λ (x) (> 0 x)) (list -2 -1 0 1 2))
- (filter (λ (x) (not (>= x 0))) (list -2 -1 0 1 2))
- (filter (λ (x) (not (<= 0 x))) (list -2 -1 0 1 2))
- (filter negative? (list -2 -1 0 1 2))


Expand Down
40 changes: 35 additions & 5 deletions default-recommendations/numeric-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,46 @@
sub1)


(define-refactoring-rule zero-comparison-to-positive?
#:description "This expression is equivalent to calling the `positive?` predicate."
#:literals (< > <= >= not)
(~or (> e:expr 0)
(< 0 e:expr)
(not (<= e:expr 0))
(not (>= 0 e:expr)))
(positive? e))


(define-refactoring-rule zero-comparison-lambda-to-positive?
#:description "This lambda function is equivalent to the built-in `positive?` predicate."
#:literals (< >)
(lambda:lambda-by-any-name (x1:id) (~or (> x2:id 0) (< 0 x2:id)))
#:literals (< > <= >= not)
(lambda:lambda-by-any-name (x1:id)
(~or (> x2:id 0)
(< 0 x2:id)
(not (<= x2:id 0))
(not (>= 0 x2:id))))
#:when (free-identifier=? #'x1 #'x2)
positive?)


(define-refactoring-rule zero-comparison-to-negative?
#:description "This expression is equivalent to calling the `negative?` predicate."
#:literals (< > <= >= not)
(~or (< e:expr 0)
(> 0 e:expr)
(not (>= e:expr 0))
(not (<= 0 e:expr)))
(negative? e))


(define-refactoring-rule zero-comparison-lambda-to-negative?
#:description "This lambda function is equivalent to the built-in `negative?` predicate."
#:literals (< >)
(lambda:lambda-by-any-name (x1:id) (~or (< x2:id 0) (> 0 x2:id)))
#:literals (< > <= >= not)
(lambda:lambda-by-any-name (x1:id)
(~or (< x2:id 0)
(> 0 x2:id)
(not (>= x2:id 0))
(not (<= 0 x2:id))))
#:when (free-identifier=? #'x1 #'x2)
negative?)

Expand All @@ -70,4 +98,6 @@
single-argument-plus-to-identity
sub1-lambda-to-sub1
zero-comparison-lambda-to-negative?
zero-comparison-lambda-to-positive?))
zero-comparison-lambda-to-positive?
zero-comparison-to-negative?
zero-comparison-to-positive?))

0 comments on commit 696e9ba

Please sign in to comment.