Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 5, 2025

  • Explore and understand the existing refactoring rule structure
  • Create test file for fuse-map-with-for rule
  • Implement the fuse-map-with-for refactoring rule
  • Add the new rule to default-recommendations
  • Run tests to verify implementation - all tests passing
  • Code review - addressed feedback
  • Security check - no issues found
  • Address PR comments - fixed rule logic and added missing tests

Summary

Implemented the fuse-map-with-for refactoring rule that fuses map expressions with for loops when the intermediate list is only used once. The rule now correctly:

  • For loops*: Fuses with ANY for* loop (single or multi-clause) by adding nested clauses
  • For loops: Only fuses with SINGLE-clause for loops using internal definitions (multi-clause for loops iterate in parallel and cannot be fused)

All test cases pass including the newly added tests for single-clause for* loops and multi-clause for loops.

Original prompt

This section details on the original issue you should resolve

<issue_title>New rule: fuse-map-with-for</issue_title>
<issue_description>### Rule summary

A map expression that produces a list which is immediately iterated over by a for* loop can be fused with the for* loop, if the intermediate list isn't used elsewhere. This avoids constructing the intermediate list and is usually more readable. This should only be applied when the map expression is given a short lambda.

Test case

#lang resyntax/test

test: "original code should be refactorable to new code"
--------------------
#lang racket
(define (f xs g h)
  (define ys (map (λ (x) (g x)) xs))
  (for* ([y (in-list ys)]
         [z (in-list (h y))])
    (displayln z)))
====================
#lang racket
(define (f xs g h)
  (for* ([x (in-list xs)]
         [y (in-list (g x))]
         [z (in-list (h y))])
    (displayln z)))
--------------------

No-change test case

#lang resyntax/test

no-change-test: "code not refactorable"
--------------------
#lang racket
(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))
--------------------

Additional context

Spotted this here in racket/drracket#783.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits December 5, 2025 17:28
Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com>
Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com>
Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com>
Copilot AI changed the title [WIP] Add new rule to fuse map with for loop Add fuse-map-with-for refactoring rule Dec 5, 2025
Copilot AI requested a review from jackfirth December 5, 2025 17:42
@jackfirth jackfirth marked this pull request as ready for review January 5, 2026 04:51
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 3 files in this pull request and found no issues.

Copy link
Owner

@jackfirth jackfirth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coveralls
Copy link

Coverage Status

coverage: 93.676% (+0.01%) from 93.662%
when pulling 9580e5e on copilot/add-fuse-map-with-for-rule
into 9dbfa96 on master.

- 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>
Copilot AI requested a review from jackfirth January 5, 2026 05:13
@jackfirth jackfirth enabled auto-merge (squash) January 5, 2026 05:35
@jackfirth jackfirth merged commit 4d3ff74 into master Jan 5, 2026
5 checks passed
@jackfirth jackfirth deleted the copilot/add-fuse-map-with-for-rule branch January 5, 2026 05:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New rule: fuse-map-with-for

3 participants