resyntax icon indicating copy to clipboard operation
resyntax copied to clipboard

when in for loop

Open sorawee opened this issue 2 years ago • 3 comments

Rewrites

(for (....)
  (when ... .....))

to

(for (....
      #:when ...)
  ....)

sorawee avatar Aug 30 '23 17:08 sorawee

Personally, I don't like this transformation. Syntactically, the test gets substantial rightward drift even though the body is less indented. The intent is also less clear because the #:when clause behaves in a rather subtle way.

(I got caught by a logical mistake with a #:when clause last, but I can't recall what it was.)

Metaxal avatar Aug 30 '23 19:08 Metaxal

This is probably more helpful:

(for (clause1 ...)
  (when test
    (for (clause2 ...)
      body ...)))
;; ->
(for (clause1 ...
      #:when test
      clause2 ...)
  body ...)

But I think @jackfirth wants to see an example in the wild before adding a rule.

A very beneficial use case would be for code that tries to do for/list with #:when but using nested for/list and append instead, or using an external accumulator. The #:when version is a big save there.

Metaxal avatar Aug 31 '23 06:08 Metaxal

I like this rule, since it unlocks further for loop simplifications. For example, that for loop flattening that @Metaxal suggested is already implemented. The (for ... (when ... (for ...))) example would be simplified in two passes if we had this rule.

I'm sympathetic to the idea that #:when is a bit less intuitive than (when ...). Perhaps if we limited this to cases where the condition expression is short? Limiting it only to cases that unlock further simplifications is also doable, but it's more work.

jackfirth avatar Aug 31 '23 06:08 jackfirth

I implemented this in #249 and forgot that this issue exists 😅

I implemented it because I hit a few loops in real-world code that this would simplify. It appears that complex condition expressions in when and unless aren't too common.

jackfirth avatar Aug 31 '24 08:08 jackfirth