Implicit forEach Loops in Rogue
A few weeks ago I added a new forEach
variation to Rogue that I've been finding very useful: an implicit forEach.
Fairly often I need a simple forEach
loop that just does one thing with each item in a list, for example either adding it to a result or calling a method on it:
forEach (n in list) sum += n
forEach (actor in actors) actor.update
It struck me that naming a control variable just to be able to use it once is exactly the kind of repetitive boilerplate that I like to avoid, and that's when I came up with this alternative syntax:
sum += (forEach in list)
(forEach in actors).update
Any time an implicit forEach
term is encountered, the statement containing that term is wrapped in a standard forEach
loop with a auto-generated control variable name and the term itself is replaced by the control variable name.
Here is the forEach
documentation on the project Wiki: