Strange Loopiness by Zed Lopez
This is experimental and mostly made out of the undocumented features in the Standard Rules the docs call on us not to use.
Adds while and until loops. Among other things, this makes it easy to loop indefinitely until a condition is met, which comes in handy sometimes. On the other hand, this makes it easy to loop indefinitely.
Inform 7's existing repeat statements make it hard to infinitely loop, which I'm guessing was a deliberate design decision. So be warned that these loops are powerful tools whose power is by default pointed at your foot: make sure your loop incrementally approaches its exit condition.
If you Include If True by Zed Lopez, then while and until will be able to take plain truth states instead of full conditionals.
Infinitely loop (your obligation to break out):
``` repeat while true: ```
Repeat down across a range:
``` repeat with x running from 12 down to 2: ```
``` repeat with hue running from indigo down to orange: ```
For all of the loops, you can also provide a loop index variable. Don't alter the loop index variable manually or it'll cease being a useful loop index variable.
Enumerated values in a specified range:
``` repeat with the-color running from red to blue with index i: ```
Descriptions of objects:
``` repeat with closed-thing running through closed things with index i: ```
Lists:
``` repeat with item running through L with index i: ```
To save you the trouble of reversing a list you want to iterate through, you can specify backwards.
``` repeat for item in L backwards: ```
or
``` repeat for item in L in reverse: ```
Tables with row number:
``` repeat through Table of Regrets with row number row-id; ```
But when you're going through a table backwards or sorting by a column, the row number will typically be different from a count of iterations through the loop, so if you really want the latter, too:
``` repeat through Table of Regrets with row number row-id and index i; ```
(There isn't a provided option for just the index without the row number.)
The compiler does magic I can't reproduce to let the same phrase preamble work for descriptions of objects and descriptions of enumerated values. So the loop over a description of values with index case only works for objects. If you're working with enumerated values and you want the index, do your choice of the following:
``` repeat with the-color running through colors: let index be the-color cast as a number; ```
or:
``` repeat for the-color from the first value in colors to the last value in colors with index: ```
or: ``` repeat for the-color from red to yellow with index i begin;
Definition: a color (called C) is infragreen if C < green.
let i be 0; repeat for c in infragreen colors begin; increment i; ```
Reserve this form for descriptions of objects:
``` repeat with drafty running through open containers with index i: ```
Be warned that if you do try using it with enumerated values, there *won't* be an error: the body of the loop won't be executed; it will just return immediately.
Also modifies existing syntax:
"repeat running through" and "repeat through" are interchangeable (i.e., "running" is permissible while looping through tables, and omitting it is permissible everywhere)
"for" may replace "with"
"in" may replace "from"
"in" may replace "through"
So all of the following are equivalent:
Repeat with i running from 1 to 10 Repeat with i from 1 to 10 Repeat for i in 1 to 10
And these are equivalent:
Repeat running through the Table of Regrets: Repeat through the Table of Regrets: Repeat in the Table of Regrets:
other examples:
repeat through the table of regrets backwards by decision-badness using row number row-id
Section Changelog
1/211027 Added repeat n times loop. Improved documentation. Added looping through lists backwards.
1/210930 Add separate case for repeat through list of text with index case, which had been failing. Any list of a block-valued kind other than text will still fail.
1/210928 Use I6-native do-until loop; prior implementation could break with multiple until loops