loop with early return
IR0NSIGHT opened this issue · comments
Feature request:
a for loop with ability to return early/break
Right now there is no such feature (afaik):
each(n,m) executes from start to finish, no option to break early.
In order to use a return early functionality (as is dictated in my case by the existing are-we-fast-yet benchmarks) i have to fall back to "while (run)" ... and mutate run in the loop:
var run = true
while (run) {
if (...) { run = false}
}
This is much harder to read/understand/verify and invites bugs (mutable state and unnecessary while loop)
so:
each() with break (and continue) would be nice to have.
(any kind of early return would suffice, it doesnt have to be "break")
Thanks for the issue!
I'm putting some additional context below:
Existing related work
The ML backend has a Control
effect:
effekt/libraries/ml/effekt.effekt
Lines 214 to 217 in 11218d9
and a function each
that allows one to use the Control
effect and handles it correctly:
effekt/libraries/ml/effekt.effekt
Line 230 in 11218d9
I don't know if we want to port this version of each
to the JavaScript backend as well 🤔
Related to #309
Examples
effekt/examples/ml/lib/loops.effekt
Lines 1 to 17 in 11218d9