You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the recur loop existed (and we also had for/else, #1247), I could write the loop at the end of this program like
Less spoilery, but uses some code from above
recur state = { value: 0, +enabled } while i < input#
for [regex, callback] of instructions
if match? := regex.exec input[i...]
i += match.0#
break with callback match, state
else
++i
state
... now that I have that code in front of me, I'm less compelled by this particular example, but I'm still submitting this as it seems useful.
The text was updated successfully, but these errors were encountered:
Interesting. This is like Array.prototype.reduce. Our current for reductions don't let you access the currently accumulated value (x in the first example). I could imagine using for.value for this, something like this:
y :=forreduce[0](;condition;)ffor.valuegfor.value
Granted that's uglier, and for.value isn't a thing yet. Maybe we could introduce a general syntax for naming the for loop value (similar to the |x> proposal for pipes with a named argument). For example:
y :=for[x]reduce[0](;condition;)fxgx
Note that I'm using (;condition;) because we don't support reductions in while loops, only for loops; for (;condition;) is the functional equivalent of while condition. Someday I'd hope we can use reductions directly in while loops, but we'd need a syntax that ideally doesn't take away from existing loops.
TL;DR: My suggestion is a loop like:
Based on my answer for part 2 of today's Advent of code challenge
Just a complete answer for Advent of Code 2024 day 3 part 2
If the
recur
loop existed (and we also hadfor
/else
, #1247), I could write the loop at the end of this program likeLess spoilery, but uses some code from above
... now that I have that code in front of me, I'm less compelled by this particular example, but I'm still submitting this as it seems useful.
The text was updated successfully, but these errors were encountered: