Skip to content

Commit

Permalink
Add for loop shortcuts for #:when and #:unless
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfirth committed Aug 30, 2024
1 parent 9c9992c commit 4035441
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
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)))

0 comments on commit 4035441

Please sign in to comment.