Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some shortcuts for remove* and friends #233

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions default-recommendations/list-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions default-recommendations/list-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <)
Expand Down Expand Up @@ -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)))
Loading