Strange Loopiness, Text Loops, If True

I just pushed three extensions to the Friends of I7: Strange Loopiness, Text Loops, If True. (They were all one thing in development, but I carved 'em up to make it easier for people to avoid including more than they might want.)

Strange Loopiness adds several loop options: repeat n times, until loops, loops counting down. For repeating through a table, you can specify a non-existing numeric loop variable that will have the row id, and for all of the loops, you can specify a non-existing numeric loop counter variable (it counts starting at one). It also offers syntactic alternatives to the existing repeat loops.

If True simply allows if and unless to take a plain truth state instead of a whole conditional, i.e., you can say if done and not need to say if done is true. If you include If True and Strange Loopiness, then while and until loops can also take truth states.

And Text Loops lets you loop through text like this (with loop counters available like in Strange Loopiness):

repeat with p in paragraphs of paragraph-text begin;
  repeat with l in lines of p with index line-num begin;
    repeat with word in uwords of l begin; [uword is unpunctuated word; pword is punctuated word]
      repeat with c in chars of word begin; [ chars or characters works ]

(Don’t mind the begin... end repeat in the examples – you can use colons and indentation instead as you prefer.)

Likewise, repeat loops and while loops can take index variables.

   let item be first thing held by box;
   while item is not nothing with index i begin;
      say "[item] [i] ";
      now item is the next thing held after item;
    end while;

The new loop types all begin with repeat

    let c be first value of colors;
    repeat until c is first value of colors begin;
      say "[c] ";
    end repeat;

  repeat 3 times begin;
    say "ha ";
  end repeat;

For anyone unfamiliar with them, with an until loop the condition is tested at the end of the loop instead of the beginning, so control always flows through the body of the loop at least once. In the repeat until example above it’ll say the first color and then terminate.

Occasionally, it’s useful to deliberately make an infinite loop that you escape only by breaking when some particular condition is met. With If True this is easy: while true: So the good news is it’s easy to set up an infinite loop, and the bad news is it’s easy to set up an infinite loop. Be careful that your while (or until) loop predictably gets incrementally closer to its end condition.

And there are options to make loops less verbose:

repeat for x in 12 down to 2 [...]
repeat for open-door in open doors [...]
repeat for c in colors [...]

or even more verbose…

repeat running through the Table of Words sorted by backwards word-length order with row number row-id and index i [...]

One limitation I’m not happy about is that I couldn’t make repeating through a description of values with an index work for descriptions of both enumerated values and objects. It seems like the compiler does magic to make the one description of values of kind K phrase work for both. So the with index case works only for objects. You can pass it a description of enumerated values and there’ll be no errors, but it will return immediately, finding no objects that match the description. If anyone can think of a way to make this work, please let me know. The documentation offers several workarounds for the enumerated values case.

There’s pretty much nothing here that isn’t made of the undocumented features in the Standard Rules that the docs warn us against using. I’ve done basic testing of each and every phrase and (excepting the set description with index issue above) I don’t know of any problems. But surely they exist.

Text Loops depends on 6M62 Patches by Friends of I7; it’s the only one with an external dependency.



1 Like

I’ve pushed new versions of If True and Strange Loopiness to fix a problem with until loops that @otistdog spotted and suggested a fix for – thanks, Otis.


I’ve found a bug in Strange Loopiness: repeating-with-an-index through a list worked for objects, enumerated values, arithmetic values, but failed for texts. It was easy enough to fix the texts case, and I’ve pushed that change.

To repeat with/for (loopvar - nonexisting text variable)
  running/-- through/in (L - list of texts) with/using index (index - nonexisting number variable) begin -- end loop:
        (- {-my:1} = LIST_OF_TY_GetLength({L});
                for ({index} = 1 : {index} <= {-my:1} : {index}++ )
if (BlkValueCopy({-by-reference:loopvar}, LIST_OF_TY_GetItem({L}, {index})))

… but it’d fail for any non-text block-valued kinds. But I’m not sure how big a concern that is… are there any non-text block-valued kinds? I imagine there could be with custom I6 code to create them, but I wouldn’t feel too bad about that being an unsupported case.