Skip to content

Commit

Permalink
Add some shortcuts for remove* and friends (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfirth authored Apr 17, 2024
1 parent 081a31d commit 76949ae
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
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)))

0 comments on commit 76949ae

Please sign in to comment.