rewrite-clj icon indicating copy to clipboard operation
rewrite-clj copied to clipboard

Feature request: Short circuit prewalk

Open KingMob opened this issue 2 years ago • 3 comments

Problem/Opportunity It would be handy in some cases to short-circuit a prewalk early. E.g., if there's an :uneval reader literal or a comment block, I'd prefer to stop drilling down in that case.

Proposed Solution Currently the walk visiting fns should return either a zipper (usually with changes) or nil, to indicate no changes. One way to handle this, then, is to check the returned value with reduced?, and if true, stop processing any further down that subtree.

Alternative Solutions

  1. A sentinel value of some sort could also be returned, but reduced fits existing short-circuiting methods better, and allows you still submit a changed subtree after you've decided you're done with it.
  2. An additional, separate predicate could be used to test for continued subtree processing, and while it would separate the logic between "continue-down-this-subtree" and "this-node-is-interesting", it would require an additional arity, or other changes to the prewalk fn signature.

Additional context I'm not really coming at this from an editing or linter-fixing angle, I'm more interested in code behavior alteration, and being able to exclude unevaluated subtrees would be handy. In my case, I can't ignore them and rely on the code never being run, because I build lists of fns as I go along, so they still have to be excluded.

Currently, I locate candidate fns, and then search again upwards to see if they're contained in a uneval node or a comment block.

Action I'm happy to help with any aspect of the solution, though I'm still new to rewrite-clj.

KingMob avatar Jan 27 '23 09:01 KingMob

Hi @KingMob! Thanks for raising a feature request.

While I think your idea is interesting, I'm also thinking maybe we could figure out a way to do what you want to do more efficiently without modifying rewrite-clj.

Also when I read:

Currently, I locate candidate fns, and then search again upwards to see if they're contained in a uneval node or a comment block.

I'm not sure, but this feels like the very old #70. Which I did spend some time thinking about recently-ish.

lread avatar Jan 27 '23 16:01 lread

I must have missed #70 somehow; it seems like it covers my use case, assuming it skips over the whole subtree, and not just that particular node (it's not clear to me that's the case, but I'm still new at rewrite-clj).

I added some comments on your brainstorming doc, but tl;dr: do we need a general skipping ability, or have most people only asked to skip uneval/comment?

KingMob avatar Jan 29 '23 09:01 KingMob

Cool, thanks @KingMob, I'll follow up in #199.

lread avatar Jan 29 '23 16:01 lread