jackfirth / resyntax

A Racket refactoring engine
Apache License 2.0
51 stars 10 forks source link

Use #:result in for/fold #215

Open sorawee opened 11 months ago

sorawee commented 11 months ago

This one may be difficult, but might be worth trying.

Previously, for/fold doesn't support #:result, so the code could look like this:

(define (my-fun)
  .....
  (define-values (a b) 
    (for/fold ([a ...] [b ...]) (....)
       .....))
  (+ a b))

But with #:result, we can simplify it to simply:

(define (my-fun)
  .....
  (for/fold ([a ...] [b ...] #:result (+ a b)) (....)
     .....)

More generally, the any define-values with for/fold expression can move the single "tail computation" expression into #:result and remove the wrapping define-values. It's worth doing this when the "tail computation" is simple.

jackfirth commented 11 months ago

That is indeed tricky. Got some examples of code in the wild this could clean up? Maybe we could get more mileage out of only detecting simpler cases, like when only one of the define-values bindings is used.