repeat over, #item, #last, #first

control structure

Syntax
repeat over reversed? shelf (as alias)? (& reversed? shelf (as alias)?)*
   local-scope
again
    


Purpose

You can use repeat over to process each item on a shelf in turn. The following program creates a shelf with five items and outputs each item in turn:

  process
     local integer digits initial { 1, 2, 3, 4, 5 }
  
     repeat over digits
        output "d" % digits || "%n"
     again

Within the body of the repeat over loop, the current item on the shelf is advanced one position for each iteration of the loop. This means that you can refer to the current item by the name of the shelf alone, without an indexer, as in the example above. You can still reference other items on the shelf using indexers. If the reversed qualifier is specified, the iteration proceeds in reverse, beginning with the last item on the shelf.

You can repeat over more that one shelf at a time, providing that all the shelves are the same size:

  process
     local integer digits  initial { 1, 2, 3, 4, 5 }
     local string  letters initial { "A", "B", "C", "D", "E" }
  
     repeat over digits & letters
        output letters
            || "d" % digits
            || "%n"
     again

Within a repeat over loop, three built-in, single-item shelves are available to help you determine which iteration is being executed. #first and #last are switch shelves that are true when the loop is in the first and last iterations respectively:

  process
     local integer digits  initial { 1, 2, 3, 4, 5 }
     local string  letters initial { "A", "B", "C", "D", "E" }
  
     repeat over digits & letters
        output "[" 
           when #first
        output letters || "d" % digits
        output ", " 
           unless #last
        output  "]%n" 
           when #last
     again

#item is an integer corresponding to the number of iterations of the loop, starting at 1 (one):

  process
     local string letters initial { "A", "B", "C", "D", "E" }
  
     repeat over letters
        output "[" 
           when #first
        output letters || "d" % #item
        output ", " 
           unless #last
        output  "]%n" 
           when #last
     again

You can specify an alias for the shelf that you are repeating over. The alias name is specified using the as keyword following the name of the shelf. Specifying an alias is required when you iterate over a shelf that is a field of a record or the result of a shelf-class function call:

  declare record T
     field string letters initial { "A", "B", "C", "D", "E" }
  
  process
     local T x
  
     repeat over x:letters as l
        output "[" 
           when #first
        output l || "d" % #item
        output ", " 
           unless #last
        output  "]%n" 
           when #last
     again

Notes

A using statement inside a repeat over loop will reset the current item to that specified in the using but will not affect the sequence or number of iterations.

You can add an item to a shelf inside a repeat over loop, but only at a position greater that the size of the shelf when the loop began. The number of iterations in the loop is determined before the first iteration, and adding more items to the shelf from within the loop will not change the number of iterations being performed. You can remove items from a shelf within a repeat over loop, but only items that were added within the same loop.

repeat over can also be used to process the following: