diff --git a/default-recommendations/numeric-shortcuts-test.rkt b/default-recommendations/numeric-shortcuts-test.rkt index 8009280..434d1e9 100644 --- a/default-recommendations/numeric-shortcuts-test.rkt +++ b/default-recommendations/numeric-shortcuts-test.rkt @@ -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)) diff --git a/default-recommendations/numeric-shortcuts.rkt b/default-recommendations/numeric-shortcuts.rkt index 520b502..3166b0b 100644 --- a/default-recommendations/numeric-shortcuts.rkt +++ b/default-recommendations/numeric-shortcuts.rkt @@ -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?) @@ -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?))