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 for loop shortcuts for #:when and #:unless #249

Merged
merged 1 commit into from
Aug 30, 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
52 changes: 52 additions & 0 deletions default-recommendations/for-loop-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,55 @@ test: "(apply append ...) with a multi-body for* loop can't be removed"
(displayln "formula time!")
(hash-ref formulas k)))
------------------------------------------------------------


test: "(when ...) in a for loop refactored to #:when clause"
------------------------------------------------------------
(for ([x (in-list (list 1 2 'a 3 'b 4))])
(when (number? x)
(displayln x)))
------------------------------------------------------------
------------------------------------------------------------
(for ([x (in-list (list 1 2 'a 3 'b 4))]
#:when (number? x))
(displayln x))
------------------------------------------------------------


test: "(when ...) in a for* loop refactored to #:when clause"
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
(when (number? x)
(displayln x)))
------------------------------------------------------------
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))]
#:when (number? x))
(displayln x))
------------------------------------------------------------


test: "(unless ...) in a for loop refactored to #:when clause"
------------------------------------------------------------
(for ([x (in-list (list 1 2 'a 3 'b 4))])
(unless (number? x)
(displayln x)))
------------------------------------------------------------
------------------------------------------------------------
(for ([x (in-list (list 1 2 'a 3 'b 4))]
#:unless (number? x))
(displayln x))
------------------------------------------------------------


test: "(unless ...) in a for* loop refactored to #:when clause"
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
(unless (number? x)
(displayln x)))
------------------------------------------------------------
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))]
#:unless (number? x))
(displayln x))
------------------------------------------------------------
20 changes: 18 additions & 2 deletions default-recommendations/for-loop-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,26 @@

(define-refactoring-rule apply-append-for-loop-to-for-loop
#:description "Instead of using `(apply append ...)` to flatten a list of lists, consider using\
`for*/list` to flatten the list"
`for*/list` to flatten the list."
#:literals (apply append)
[(apply append loop:apply-append-refactorable-for-loop)
loop.refactored-loop])


(define-refactoring-rule when-expression-in-for-loop-to-when-keyword
#:description "Use the `#:when` keyword instead of `when` to reduce loop body indentation."
#:literals (when for for*)
[((~or for-id:for for-id:for*) (clause ...) (when condition body ...))
(for-id (clause ... #:when condition) body ...)])


(define-refactoring-rule unless-expression-in-for-loop-to-unless-keyword
#:description "Use the `#:unless` keyword instead of `unless` to reduce loop body indentation."
#:literals (unless for for*)
[((~or for-id:for for-id:for*) (clause ...) (unless condition body ...))
(for-id (clause ... #:unless condition) body ...)])


(define for-loop-shortcuts
(refactoring-suite
#:name (name for-loop-shortcuts)
Expand All @@ -292,4 +306,6 @@
named-let-loop-to-for/first-in-vector
nested-for-to-for*
or-in-for/and-to-filter-clause
ormap-to-for/or)))
ormap-to-for/or
unless-expression-in-for-loop-to-unless-keyword
when-expression-in-for-loop-to-when-keyword)))
Loading