Skip to content

Commit 9a30ca0

Browse files
authored
Add build-list-to-for rule (#473)
Closes #384.
1 parent e0e361e commit 9a30ca0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

default-recommendations/for-loop-shortcuts-test.rkt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,44 @@ test: "hash-for-each with let expression refactorable to for with definitions"
262262
------------------------------
263263

264264

265+
test: "build-list with short single-body form not refactorable"
266+
- (build-list 10 (λ (i) (* i 2)))
267+
268+
269+
test: "build-list with long single-body form refactorable to for/list"
270+
------------------------------
271+
(build-list 10
272+
(λ (a-very-very-very-long-variable-name-thats-so-very-long)
273+
(* a-very-very-very-long-variable-name-thats-so-very-long 2)))
274+
------------------------------
275+
------------------------------
276+
(for/list ([a-very-very-very-long-variable-name-thats-so-very-long (in-range 10)])
277+
(* a-very-very-very-long-variable-name-thats-so-very-long 2))
278+
------------------------------
279+
280+
281+
test: "build-list with multiple body forms refactorable to for/list"
282+
------------------------------
283+
(build-list 10 (λ (i) (displayln i) (* i 2)))
284+
------------------------------
285+
------------------------------
286+
(for/list ([i (in-range 10)])
287+
(displayln i)
288+
(* i 2))
289+
------------------------------
290+
291+
292+
test: "build-list with let expression refactorable to for/list"
293+
------------------------------
294+
(build-list 10 (λ (i) (let ([j (* i 2)]) (list i j))))
295+
------------------------------
296+
------------------------------
297+
(for/list ([i (in-range 10)])
298+
(define j (* i 2))
299+
(list i j))
300+
------------------------------
301+
302+
265303
test: "ormap to for/or"
266304
------------------------------
267305
(define some-list (list 3 5 14 10 6 5 2))

default-recommendations/for-loop-shortcuts.rkt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@
176176
function.body ...))
177177

178178

179+
(define-refactoring-rule build-list-to-for
180+
#:description "This `build-list` operation can be replaced with a `for/list` loop."
181+
#:literals (build-list)
182+
(build-list n function:worthwhile-loop-body-function)
183+
(for/list ([function.x (in-range n)])
184+
function.body ...))
185+
186+
179187
(define-syntax-class for-loop-supporting-leading-nested-clause
180188
#:literals (for/list for*/list)
181189
#:attributes ([clause 1] [body 1])
@@ -530,6 +538,7 @@ return just that result."
530538
#:rules (andmap-to-for/and
531539
append-map-for/list-to-for*/list
532540
apply-plus-to-for/sum
541+
build-list-to-for
533542
for/fold-building-hash-to-for/hash
534543
for/fold-result-keyword
535544
for/fold-with-conditional-body-to-unless-keyword

0 commit comments

Comments
 (0)