loop

The loop keyword offers a mechanism to repeat a short series of Envision operations. The maximal number of iterations is 10. This low maximum is intentional as arbitrary loops introduce problems that are better solved through the other constructs of the Envision language.

See iterating with ’loop’ for detailed usage documentation.

loop (n : number), code block

The loop keyword takes a single argument which must be an integer constant between 1 and 10 (inclusive). The count may be any constant expression.

a = 1
loop 3
  b = a + 1
  a = 2 * b
show summary "" with a, b // 22, 11

loop does not introduce a new scope; variables defined inside remain visible. The block is a macro expansion repeated n times.

loop (n : number) in (low : number) .. (high : number), code block

The loop keyword defines an iteration variable over a range of integer values. The boundaries are inclusive, and must contain between 1 and 10 values.

s = 0
loop n in 11 .. 15
  s = s + n
show summary "" with s // 65

loop (n : number) in sequence, code block

The loop keyword defines an iteration variable over an explicit list of values. The list length (and thus the iteration count) is capped at 10.

msg = ""
loop n in ("h", "e", "l", "l", "o")
  msg = concat(msg, n)
show summary "" with msg // 'hello'

The list can contain arbitrary expressions and tuple unpacking.

Values = ("B", "C", "D")
Concat = ""
loop V in ("A", ...Values, "E")
  Concat = "\{Concat}\{V}"
show scalar "" with Concat // ABCDE

Restrictions

loop blocks cannot include import, def, show, read, write, or nested loop statements. Table declarations are allowed only when they re-apply a filter to the same table across iterations.

a = 10
b = 0
table T = extend.range(10)
loop 3
  a = a - 1
  table T = where T.N < a
  b = b + sum(T.N)

loop can be used inside table comprehensions:

table T = with
  [| as A |]
  loop N in 1..3
    [| N |]
User Contributed Notes
0 notes + add a note