generated from jackfirth/racket-package-template
-
Notifications
You must be signed in to change notification settings - Fork 12
Add fuse-map-with-for refactoring rule #778
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
Merged
+221
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3c547a0
Initial plan
Copilot 98962bc
WIP: fuse-map-with-for rule implementation in progress
Copilot 582ccbd
Complete implementation of fuse-map-with-for rule
Copilot 9580e5e
Fix let expression result-form handling and add missing require
Copilot bdf892a
Fix rule to only fuse for* loops and single-clause for loops
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
default-recommendations/loops/fuse-map-with-for-test.rkt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| #lang resyntax/test | ||
|
|
||
|
|
||
| require: resyntax/default-recommendations/loops/fuse-map-with-for fuse-map-with-for | ||
|
|
||
|
|
||
| header: | ||
| - #lang racket/base | ||
|
|
||
|
|
||
| test: "map producing list for for* loop can be fused" | ||
| -------------------- | ||
| (define (f xs g h) | ||
| (define ys (map (λ (x) (g x)) xs)) | ||
| (for* ([y (in-list ys)] | ||
| [z (in-list (h y))]) | ||
| (displayln z))) | ||
| ==================== | ||
| (define (f xs g h) | ||
| (for* ([x (in-list xs)] | ||
| [y (in-list (g x))] | ||
| [z (in-list (h y))]) | ||
| (displayln z))) | ||
| -------------------- | ||
|
|
||
|
|
||
| test: "map producing list for single-clause for* loop can be fused" | ||
| -------------------- | ||
| (define (f xs g) | ||
| (define ys (map (λ (x) (g x)) xs)) | ||
| (for* ([y (in-list ys)]) | ||
| (displayln y))) | ||
| ==================== | ||
| (define (f xs g) | ||
| (for* ([x (in-list xs)] | ||
| [y (in-list (g x))]) | ||
| (displayln y))) | ||
| -------------------- | ||
|
|
||
|
|
||
| test: "map producing list for for loop can be fused" | ||
| -------------------- | ||
| (define (f xs g) | ||
| (define ys (map (λ (x) (g x)) xs)) | ||
| (for ([y (in-list ys)]) | ||
| (displayln y))) | ||
| ==================== | ||
| (define (f xs g) | ||
| (for ([x (in-list xs)]) | ||
| (define y (g x)) | ||
| (displayln y))) | ||
| -------------------- | ||
|
|
||
|
|
||
| no-change-test: "multi-clause for loop cannot be fused" | ||
| -------------------- | ||
| (define (f xs zs) | ||
| (define ys (map (λ (x) (+ x 1)) xs)) | ||
| (for ([y (in-list ys)] | ||
| [z (in-list zs)]) | ||
| (displayln (list y z)))) | ||
| -------------------- | ||
|
|
||
|
|
||
| no-change-test: "map with short lambda but ys used elsewhere not refactorable" | ||
| -------------------- | ||
| (define (f xs g h) | ||
| (define ys (map (λ (x) (g x)) xs)) | ||
| (for* ([y (in-list ys)] | ||
| [z (in-list (h y))]) | ||
| (displayln z)) | ||
| (displayln ys)) | ||
| -------------------- | ||
|
|
||
|
|
||
| test: "map with lambda that has multiple body forms is refactorable" | ||
| -------------------- | ||
| (define (f xs g) | ||
| (define ys (map (λ (x) (displayln x) (g x)) xs)) | ||
| (for ([y (in-list ys)]) | ||
| (displayln y))) | ||
| ==================== | ||
| (define (f xs g) | ||
| (for ([x (in-list xs)]) | ||
| (displayln x) | ||
| (define y (g x)) | ||
| (displayln y))) | ||
| -------------------- | ||
|
|
||
|
|
||
| test: "map with long single-body lambda is refactorable" | ||
| -------------------- | ||
| (define (f xs) | ||
| (define long-name 42) | ||
| (define ys | ||
| (map (λ (x) | ||
| (+ x long-name)) | ||
| xs)) | ||
| (for ([y (in-list ys)]) | ||
| (displayln y))) | ||
| ==================== | ||
| (define (f xs) | ||
| (define long-name 42) | ||
| (for ([x (in-list xs)]) | ||
| (define y (+ x long-name)) | ||
| (displayln y))) | ||
| -------------------- |
jackfirth marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #lang racket/base | ||
|
|
||
|
|
||
| (require racket/contract/base) | ||
|
|
||
|
|
||
| (provide | ||
| (contract-out | ||
| [fuse-map-with-for refactoring-suite?])) | ||
|
|
||
|
|
||
| (require resyntax/base | ||
| racket/list | ||
| resyntax/default-recommendations/analyzers/identifier-usage | ||
| resyntax/default-recommendations/let-replacement/private/let-binding | ||
| resyntax/default-recommendations/private/lambda-by-any-name | ||
| syntax/parse) | ||
|
|
||
|
|
||
| ;@---------------------------------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| ;; A short lambda suitable for fusing with a for loop. For multi-body lambdas, we need to | ||
| ;; separate the prefix forms (all but last) from the result form (the last). | ||
| (define-syntax-class fuseable-map-lambda | ||
| #:attributes (x single-body [multi-body 1] [prefix-forms 1] result-form) | ||
|
|
||
| ;; Lambdas with let expressions that can be refactored | ||
| (pattern | ||
| (_:lambda-by-any-name (x:id) | ||
| original-body:body-with-refactorable-let-expression) | ||
| #:with (multi-body ...) #'(original-body.refactored ...) | ||
| #:do [(define refactored-forms (attribute original-body.refactored)) | ||
| (define prefix-list (if (null? refactored-forms) '() (drop-right refactored-forms 1))) | ||
| (define result (if (null? refactored-forms) #'(begin) (last refactored-forms)))] | ||
| #:attr [prefix-forms 1] prefix-list | ||
| #:attr result-form result | ||
| #:attr single-body #'(begin original-body.refactored ...)) | ||
|
|
||
| ;; Lambdas with multiple body forms (two or more) | ||
| (pattern (_:lambda-by-any-name (x:id) prefix-form ... last-form) | ||
| #:when (not (null? (attribute prefix-form))) | ||
| #:with (multi-body ...) #'(prefix-form ... last-form) | ||
| #:attr [prefix-forms 1] (attribute prefix-form) | ||
| #:attr result-form #'last-form | ||
| #:attr single-body #'(begin prefix-form ... last-form)) | ||
|
|
||
| ;; Short lambdas with a single body form | ||
| (pattern (_:lambda-by-any-name (x:id) only-form) | ||
| #:with (multi-body ...) #'(only-form) | ||
| #:attr [prefix-forms 1] '() | ||
| #:attr result-form #'only-form | ||
| #:attr single-body #'only-form)) | ||
|
|
||
|
|
||
| (define-definition-context-refactoring-rule fuse-map-with-for*-rule | ||
| #:description | ||
| "A `map` expression producing a list for a `for*` loop can be fused with the loop." | ||
| #:analyzers (list identifier-usage-analyzer) | ||
| #:literals (define map in-list for*) | ||
| (~seq body-before ... | ||
| (define ys:id (map function:fuseable-map-lambda list-expr:expr)) | ||
| (for*-id:for* | ||
| (~and original-clauses | ||
| ([y-var:id (in-list ys-usage:id)] remaining-clause ...)) | ||
| for-body ...) | ||
| body-after ...) | ||
|
|
||
| ;; Check that ys is only used in the for loop, not elsewhere | ||
| #:when (free-identifier=? (attribute ys) (attribute ys-usage)) | ||
| #:when (equal? (syntax-property #'ys 'usage-count) 1) | ||
|
|
||
| ;; Generate the refactored code - fuse as nested clauses | ||
| (body-before ... | ||
| (for*-id ([function.x (in-list list-expr)] | ||
| [y-var (in-list function.single-body)] | ||
| remaining-clause ...) | ||
| for-body ...) | ||
| body-after ...)) | ||
|
|
||
|
|
||
| ;; Rule for when there are single-clause for loops (not for*) - use internal definition | ||
| (define-definition-context-refactoring-rule fuse-map-with-for-single-clause-rule | ||
| #:description | ||
| "A `map` expression producing a list for a single-clause `for` loop can be fused with the loop." | ||
| #:analyzers (list identifier-usage-analyzer) | ||
| #:literals (define map in-list for) | ||
| (~seq body-before ... | ||
| (define ys:id (map function:fuseable-map-lambda list-expr:expr)) | ||
| (for-id:for | ||
| (~and original-clauses | ||
| ([y-var:id (in-list ys-usage:id)])) | ||
| for-body ...) | ||
| body-after ...) | ||
|
|
||
| ;; Check that ys is only used in the for loop, not elsewhere | ||
| #:when (free-identifier=? (attribute ys) (attribute ys-usage)) | ||
| #:when (equal? (syntax-property #'ys 'usage-count) 1) | ||
|
|
||
| ;; Generate the refactored code - use internal definition | ||
| (body-before ... | ||
| (for-id ([function.x (in-list list-expr)]) | ||
| function.prefix-forms ... | ||
| (define y-var function.result-form) | ||
| for-body ...) | ||
| body-after ...)) | ||
|
|
||
|
|
||
| (define-refactoring-suite fuse-map-with-for | ||
| #:rules (fuse-map-with-for*-rule | ||
| fuse-map-with-for-single-clause-rule)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.