Skip to content

Commit bdf892a

Browse files
Copilotjackfirth
andcommitted
Fix rule to only fuse for* loops and single-clause for loops
- Renamed fuse-map-with-for-rule to fuse-map-with-for*-rule that matches only for* loops (any number of clauses) - Updated fuse-map-with-for-single-clause-rule to match only single-clause for loops (not for*) - Added test for single-clause for* loop fusion - Added no-change test for multi-clause for loop (cannot be fused due to parallel iteration) Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com>
1 parent 9580e5e commit bdf892a

2 files changed

Lines changed: 38 additions & 14 deletions

File tree

default-recommendations/loops/fuse-map-with-for-test.rkt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ test: "map producing list for for* loop can be fused"
2424
--------------------
2525

2626

27+
test: "map producing list for single-clause for* loop can be fused"
28+
--------------------
29+
(define (f xs g)
30+
(define ys (map (λ (x) (g x)) xs))
31+
(for* ([y (in-list ys)])
32+
(displayln y)))
33+
====================
34+
(define (f xs g)
35+
(for* ([x (in-list xs)]
36+
[y (in-list (g x))])
37+
(displayln y)))
38+
--------------------
39+
40+
2741
test: "map producing list for for loop can be fused"
2842
--------------------
2943
(define (f xs g)
@@ -38,6 +52,16 @@ test: "map producing list for for loop can be fused"
3852
--------------------
3953

4054

55+
no-change-test: "multi-clause for loop cannot be fused"
56+
--------------------
57+
(define (f xs zs)
58+
(define ys (map (λ (x) (+ x 1)) xs))
59+
(for ([y (in-list ys)]
60+
[z (in-list zs)])
61+
(displayln (list y z))))
62+
--------------------
63+
64+
4165
no-change-test: "map with short lambda but ys used elsewhere not refactorable"
4266
--------------------
4367
(define (f xs g h)

default-recommendations/loops/fuse-map-with-for.rkt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@
5353
#:attr single-body #'only-form))
5454

5555

56-
(define-definition-context-refactoring-rule fuse-map-with-for-rule
56+
(define-definition-context-refactoring-rule fuse-map-with-for*-rule
5757
#:description
58-
"A `map` expression producing a list for a `for` loop can be fused with the loop."
58+
"A `map` expression producing a list for a `for*` loop can be fused with the loop."
5959
#:analyzers (list identifier-usage-analyzer)
60-
#:literals (define map in-list for for*)
60+
#:literals (define map in-list for*)
6161
(~seq body-before ...
6262
(define ys:id (map function:fuseable-map-lambda list-expr:expr))
63-
((~or for-id:for for-id:for*)
63+
(for*-id:for*
6464
(~and original-clauses
65-
([y-var:id (in-list ys-usage:id)] remaining-clause ...+))
65+
([y-var:id (in-list ys-usage:id)] remaining-clause ...))
6666
for-body ...)
6767
body-after ...)
6868

@@ -72,22 +72,22 @@
7272

7373
;; Generate the refactored code - fuse as nested clauses
7474
(body-before ...
75-
(for-id ([function.x (in-list list-expr)]
76-
[y-var (in-list function.single-body)]
77-
remaining-clause ...)
78-
for-body ...)
75+
(for*-id ([function.x (in-list list-expr)]
76+
[y-var (in-list function.single-body)]
77+
remaining-clause ...)
78+
for-body ...)
7979
body-after ...))
8080

8181

82-
;; Rule for when there are no remaining clauses - use internal definition
82+
;; Rule for when there are single-clause for loops (not for*) - use internal definition
8383
(define-definition-context-refactoring-rule fuse-map-with-for-single-clause-rule
8484
#:description
85-
"A `map` expression producing a list for a `for` loop can be fused with the loop."
85+
"A `map` expression producing a list for a single-clause `for` loop can be fused with the loop."
8686
#:analyzers (list identifier-usage-analyzer)
87-
#:literals (define map in-list for for*)
87+
#:literals (define map in-list for)
8888
(~seq body-before ...
8989
(define ys:id (map function:fuseable-map-lambda list-expr:expr))
90-
((~or for-id:for for-id:for*)
90+
(for-id:for
9191
(~and original-clauses
9292
([y-var:id (in-list ys-usage:id)]))
9393
for-body ...)
@@ -107,5 +107,5 @@
107107

108108

109109
(define-refactoring-suite fuse-map-with-for
110-
#:rules (fuse-map-with-for-rule
110+
#:rules (fuse-map-with-for*-rule
111111
fuse-map-with-for-single-clause-rule))

0 commit comments

Comments
 (0)