resyntax icon indicating copy to clipboard operation
resyntax copied to clipboard

Replace `for` and `set!` with `for/fold`

Open jackfirth opened this issue 1 year ago • 0 comments

Saw some code in racket/drracket#691 where a rule like this would help:

#lang resyntax/test

test: "for with set! refactorable to for/fold"
--------------------
#lang racket
(define (f n)
  (define xs '())
  (for ([x (in-range n)])
    (set! xs (cons x xs)))
  xs)
--------------------
--------------------
#lang racket
(define f n
  (define xs
    (for/fold ([xs '()])
              ([x (in-range n)])
      (cons x xs)))
  xs)
--------------------

Might be some subtleties to this one.

jackfirth avatar Nov 05 '24 00:11 jackfirth