Skip to content
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
31 changes: 27 additions & 4 deletions default-recommendations/for-loop-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,31 @@ test: "nested for/and forms can be flattened to a for*/and form"
------------------------------


test: "named let loop over counter can be replaced by for in-range"
------------------------------------------------------------
(define (f x a b dx)
(let loop ([x a])
(when (< x b)
(displayln x)
(loop (+ x dx)))))
============================================================
(define (f x a b dx)
(for ([x (in-range a b dx)])
(displayln x)))
------------------------------------------------------------


no-change-test: "named let loop over counter not refactorable when loop function used elsewhere"
------------------------------------------------------------
(define (f x a b dx)
(let loop ([x a])
(when (< x b)
(displayln x)
(displayln loop)
(loop (+ x dx)))))
------------------------------------------------------------


test: "named let loop with conditional return over vector can be replaced by for/first"
------------------------------------------------------------
(define vec (vector 0 1 2 3 4 5))
Expand All @@ -633,17 +658,15 @@ test: "named let loop with conditional return over vector can be replaced by for
(if (> x 3)
(+ x 42)
(loop (add1 i))))))
------------------------------------------------------------
------------------------------------------------------------
============================================================
(define vec (vector 0 1 2 3 4 5))
(let loop ([i 0])
(and (< i (vector-length vec))
(let ([x (vector-ref vec i)])
(if (> x 3)
(+ x 42)
(loop (+ i 1))))))
------------------------------------------------------------
------------------------------------------------------------
============================================================
(define vec (vector 0 1 2 3 4 5))
(for/first ([x (in-vector vec)]
#:when (> x 3))
Expand Down
20 changes: 20 additions & 0 deletions default-recommendations/for-loop-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
resyntax/default-recommendations/private/lambda-by-any-name
resyntax/default-recommendations/private/let-binding
resyntax/default-recommendations/private/list-function
resyntax/default-recommendations/private/literal-constant
resyntax/default-recommendations/private/metafunction
resyntax/default-recommendations/private/syntax-equivalence
resyntax/default-recommendations/private/syntax-identifier-sets
Expand Down Expand Up @@ -436,6 +437,24 @@ return just that result."
nested.body ...))


(define-refactoring-rule named-let-loop-to-for-in-range
#:description "This named `let` expression is equivalent to a `for` loop that uses `in-range`."
#:literals (let when < +)
(let loop:id ([x:id start:expr])
(when (< x2:id (~or stop:id stop:literal-constant))
body ...+
(loop2:id (+ x3:id (~or step:id step:literal-constant)))))
#:when (free-identifier=? (attribute x) (attribute x2))
#:when (free-identifier=? (attribute x) (attribute x3))
#:when (free-identifier=? (attribute loop) (attribute loop2))
#:when (or (not (identifier? (attribute step)))
(identifier-binding-unchanged-in-context? (attribute step) this-syntax))
#:when (not (syntax-find-first #'(body ...) id:id
#:when (free-identifier=? (attribute id) (attribute loop))))
(for ([x (in-range start stop step)])
body ...))


(define-refactoring-rule named-let-loop-to-for/list
#:description "This named `let` expression is equivalent to a `for/list` loop."
#:literals (let cond else null? empty? null quote car first cdr rest cons)
Expand Down Expand Up @@ -654,6 +673,7 @@ return just that result."
list->set-to-for/set
list->vector-to-for/vector
map-to-for
named-let-loop-to-for-in-range
named-let-loop-to-for/and
named-let-loop-to-for/first-in-vector
named-let-loop-to-for/list
Expand Down