jackfirth / resyntax

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

Suggest `for/first` instead of equivalent named `let` loop over vector #155

Closed jackfirth closed 2 years ago

jackfirth commented 2 years ago

Saw this code today:

(let loop ([i (sub1 (vector-length p))])
  (and (>= i 0)
       (let ([gs (vector-ref p i)])
         (if (term? gs)
             (term-prec gs)
             (loop (sub1 i))))))

That code is equivalent to this:

(for/first ([gs (in-vector p (sub1 (vector-length p)) -1 -1)]
            #:when (term? gs))
  (term-prec gs))

There's some variants of this pattern that are also worth capturing, such as: