Skip to content

Commit 696e9ba

Browse files
authored
Expand numeric comparison shortcuts (#416)
1 parent d32b0d1 commit 696e9ba

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

default-recommendations/numeric-shortcuts-test.rkt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,35 @@ test: "lambda equivalent to sub1 refactorable to sub1"
2121
- (map sub1 (list 1 2 3))
2222

2323

24+
test: "comparison equivalent to positive? refactorable to positive?"
25+
- (> 42 0)
26+
- (< 0 42)
27+
- (not (<= 42 0))
28+
- (not (>= 0 42))
29+
- (positive? 42)
30+
31+
32+
test: "comparison equivalent to negative? refactorable to negative?"
33+
- (< 42 0)
34+
- (> 0 42)
35+
- (not (>= 42 0))
36+
- (not (<= 0 42))
37+
- (negative? 42)
38+
39+
2440
test: "lambda equivalent to positive? refactorable to positive?"
2541
- (filter (λ (x) (> x 0)) (list -2 -1 0 1 2))
2642
- (filter (λ (x) (< 0 x)) (list -2 -1 0 1 2))
43+
- (filter (λ (x) (not (<= x 0))) (list -2 -1 0 1 2))
44+
- (filter (λ (x) (not (>= 0 x))) (list -2 -1 0 1 2))
2745
- (filter positive? (list -2 -1 0 1 2))
2846

2947

3048
test: "lambda equivalent to negative? refactorable to negative?"
3149
- (filter (λ (x) (< x 0)) (list -2 -1 0 1 2))
3250
- (filter (λ (x) (> 0 x)) (list -2 -1 0 1 2))
51+
- (filter (λ (x) (not (>= x 0))) (list -2 -1 0 1 2))
52+
- (filter (λ (x) (not (<= 0 x))) (list -2 -1 0 1 2))
3353
- (filter negative? (list -2 -1 0 1 2))
3454

3555

default-recommendations/numeric-shortcuts.rkt

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,46 @@
3434
sub1)
3535

3636

37+
(define-refactoring-rule zero-comparison-to-positive?
38+
#:description "This expression is equivalent to calling the `positive?` predicate."
39+
#:literals (< > <= >= not)
40+
(~or (> e:expr 0)
41+
(< 0 e:expr)
42+
(not (<= e:expr 0))
43+
(not (>= 0 e:expr)))
44+
(positive? e))
45+
46+
3747
(define-refactoring-rule zero-comparison-lambda-to-positive?
3848
#:description "This lambda function is equivalent to the built-in `positive?` predicate."
39-
#:literals (< >)
40-
(lambda:lambda-by-any-name (x1:id) (~or (> x2:id 0) (< 0 x2:id)))
49+
#:literals (< > <= >= not)
50+
(lambda:lambda-by-any-name (x1:id)
51+
(~or (> x2:id 0)
52+
(< 0 x2:id)
53+
(not (<= x2:id 0))
54+
(not (>= 0 x2:id))))
4155
#:when (free-identifier=? #'x1 #'x2)
4256
positive?)
4357

4458

59+
(define-refactoring-rule zero-comparison-to-negative?
60+
#:description "This expression is equivalent to calling the `negative?` predicate."
61+
#:literals (< > <= >= not)
62+
(~or (< e:expr 0)
63+
(> 0 e:expr)
64+
(not (>= e:expr 0))
65+
(not (<= 0 e:expr)))
66+
(negative? e))
67+
68+
4569
(define-refactoring-rule zero-comparison-lambda-to-negative?
4670
#:description "This lambda function is equivalent to the built-in `negative?` predicate."
47-
#:literals (< >)
48-
(lambda:lambda-by-any-name (x1:id) (~or (< x2:id 0) (> 0 x2:id)))
71+
#:literals (< > <= >= not)
72+
(lambda:lambda-by-any-name (x1:id)
73+
(~or (< x2:id 0)
74+
(> 0 x2:id)
75+
(not (>= x2:id 0))
76+
(not (<= 0 x2:id))))
4977
#:when (free-identifier=? #'x1 #'x2)
5078
negative?)
5179

@@ -70,4 +98,6 @@
7098
single-argument-plus-to-identity
7199
sub1-lambda-to-sub1
72100
zero-comparison-lambda-to-negative?
73-
zero-comparison-lambda-to-positive?))
101+
zero-comparison-lambda-to-positive?
102+
zero-comparison-to-negative?
103+
zero-comparison-to-positive?))

0 commit comments

Comments
 (0)