From 76949ae1d5422bc0ce9390b775a8bdab54cb1852 Mon Sep 17 00:00:00 2001 From: Jack Firth Date: Tue, 16 Apr 2024 19:16:47 -0700 Subject: [PATCH] Add some shortcuts for `remove*` and friends (#233) --- .../list-shortcuts-test.rkt | 48 +++++++++++++++++++ default-recommendations/list-shortcuts.rkt | 41 ++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/default-recommendations/list-shortcuts-test.rkt b/default-recommendations/list-shortcuts-test.rkt index 0f97e2e..3290c78 100644 --- a/default-recommendations/list-shortcuts-test.rkt +++ b/default-recommendations/list-shortcuts-test.rkt @@ -54,6 +54,54 @@ test: "single-list append removable" - (list 1 2 3) +test: "filter with andmap and equal? to intersect lists refactorable to remove*" +------------------------------ +(define old-ids '(a b c)) +(define new-ids '(b c d e)) +(filter (λ (id) + (andmap (λ (id2) (not (equal? id id2))) + old-ids)) + new-ids) +------------------------------ +------------------------------ +(define old-ids '(a b c)) +(define new-ids '(b c d e)) +(remove* old-ids new-ids) +------------------------------ + + +test: "filter with andmap and eqv? to intersect lists refactorable to remv*" +------------------------------ +(define old-ids '(a b c)) +(define new-ids '(b c d e)) +(filter (λ (id) + (andmap (λ (id2) (not (eqv? id id2))) + old-ids)) + new-ids) +------------------------------ +------------------------------ +(define old-ids '(a b c)) +(define new-ids '(b c d e)) +(remv* old-ids new-ids) +------------------------------ + + +test: "filter with andmap and eq? to intersect lists refactorable to remq*" +------------------------------ +(define old-ids '(a b c)) +(define new-ids '(b c d e)) +(filter (λ (id) + (andmap (λ (id2) (not (eq? id id2))) + old-ids)) + new-ids) +------------------------------ +------------------------------ +(define old-ids '(a b c)) +(define new-ids '(b c d e)) +(remq* old-ids new-ids) +------------------------------ + + test: "sort by comparator using key refactorable to sort by key" ------------------------------ (define (f x) 42) diff --git a/default-recommendations/list-shortcuts.rkt b/default-recommendations/list-shortcuts.rkt index db18360..3d3842d 100644 --- a/default-recommendations/list-shortcuts.rkt +++ b/default-recommendations/list-shortcuts.rkt @@ -11,9 +11,11 @@ (require (for-syntax racket/base) racket/list + racket/set rebellion/private/static-name resyntax/default-recommendations/private/lambda-by-any-name resyntax/default-recommendations/private/literal-constant + resyntax/default-recommendations/private/syntax-identifier-sets resyntax/refactoring-rule resyntax/refactoring-suite syntax/parse) @@ -58,6 +60,42 @@ lst]) +(define-refactoring-rule filter-to-remove* + #:description + "The `remove*` function is a simpler way to remove all elements of one list from another." + #:literals (filter andmap not equal?) + [(filter (_:lambda-by-any-name (x1) (andmap (_:lambda-by-any-name (y1) (not (equal? x2 y2))) ys)) + xs) + #:when (free-identifier=? #'x1 #'x2) + #:when (free-identifier=? #'y1 #'y2) + #:when (not (set-member? (syntax-free-identifiers #'ys) #'x1)) + (remove* ys xs)]) + + +(define-refactoring-rule filter-to-remv* + #:description + "The `remv*` function is a simpler way to remove all elements of one list from another." + #:literals (filter andmap not eqv?) + [(filter (_:lambda-by-any-name (x1) (andmap (_:lambda-by-any-name (y1) (not (eqv? x2 y2))) ys)) + xs) + #:when (free-identifier=? #'x1 #'x2) + #:when (free-identifier=? #'y1 #'y2) + #:when (not (set-member? (syntax-free-identifiers #'ys) #'x1)) + (remv* ys xs)]) + + +(define-refactoring-rule filter-to-remq* + #:description + "The `remq*` function is a simpler way to remove all elements of one list from another." + #:literals (filter andmap not eq?) + [(filter (_:lambda-by-any-name (x1) (andmap (_:lambda-by-any-name (y1) (not (eq? x2 y2))) ys)) + xs) + #:when (free-identifier=? #'x1 #'x2) + #:when (free-identifier=? #'y1 #'y2) + #:when (not (set-member? (syntax-free-identifiers #'ys) #'x1)) + (remq* ys xs)]) + + (define-refactoring-rule sort-with-keyed-comparator-to-sort-by-key #:description "This `sort` expression can be replaced with a simpler, equivalent expression." #:literals (sort <) @@ -89,6 +127,9 @@ (list append-single-list-to-single-list append*-and-map-to-append-map equal-null-list-to-null-predicate + filter-to-remove* + filter-to-remq* + filter-to-remv* first-reverse-to-last quasiquote-to-list sort-with-keyed-comparator-to-sort-by-key)))