summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/lowlevel/lowlevel-loops.tex
blob: 30a3ef64cfb21562b1c089cca36d77bf765eb455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
% language=us runpath=texruns:manuals/lowlevel

\environment lowlevel-style

\startdocument
  [title=loops,
   color=middleyellow]

\startsectionlevel[title=Introduction]

I have hesitated long before I finally decided to implement native loops in
\LUAMETATEX. Among the reasons against such a feature is that one can define
macros that do loops (preferably using tail recursion). When you don't need an
expandable loop, counters can be used, otherwise there are dirty and obscure
tricks that can be of help. This is often the area where tex programmers can show
off but the fact remains that we're using side effects of the expansion machinery
and specific primitives like \type {\romannumeral} magic. In \LUAMETATEX\ it is
actually possible to use the local control mechanism to hide loop counter advance
and checking but that comes with at a performance hit. And, no matter what tricks
one used, tracing becomes pretty much cluttered.

In the next sections we describe the new native loop primitives in \LUAMETATEX\ as
well as the more traditional \CONTEXT\ loop helpers.

\stopsectionlevel

\startsectionlevel[title=Primitives]

Because \METAPOST, which is also a macro language, has native loops, it makes
sense to also have native loops in \TEX\ and in \LUAMETATEX\ it was not that hard
to add it. One variant uses the local control mechanism which is reflected in its
name and two others collect expanded bodies. In the local loop content gets
injected as we go, so this one doesn't work well in for instance an \type
{\edef}. The macro takes the usual three loop numbers as well as a token list:

\starttyping[option=TEX]
\localcontrolledloop 1 100000 1 {%
    % body
}
\stoptyping

Here is an example of usage:

\startbuffer
\localcontrolledloop 1 5 1 {%
    [\number\currentloopiterator]
    \localcontrolledloop 1 10 1 {%
        (\number\currentloopiterator)
    }%
    [\number\currentloopiterator]
    \par
}
\stopbuffer

\typebuffer[option=TEX]

The \type {\currentloopiterator} is a numeric token so you need to explicitly
serialize it with \type {\number} or \type {\the} if you want it to be typeset:

\startpacked \getbuffer \stoppacked

Here is another example. This time we also show the current nesting:

\startbuffer
\localcontrolledloop 1 100 1 {%
    \ifnum\currentloopiterator>6\relax
        \quitloop
    \else
        [\number\currentloopnesting:\number\currentloopiterator]
        \localcontrolledloop 1 8 1 {%
            (\number\currentloopnesting:\number\currentloopiterator)
        }\par
    \fi
}
\stopbuffer

\typebuffer[option=TEX]

Watch the \type {\quitloop}: it will end the loop at the {\em next} iteration so
any content after it will show up. Normally this one will be issued in a
condition and we want to end that properly.

\startpacked \getbuffer \stoppacked

The three loop variants all perform differently:

\startbuffer
l:\testfeatureonce {1000} {\localcontrolledloop 1 2000 1 {\relax}} %
  \elapsedtime
e:\testfeatureonce {1000} {\expandedloop        1 2000 1 {\relax}} %
  \elapsedtime
u:\testfeatureonce {1000} {\unexpandedloop      1 2000 1 {\relax}} %
  \elapsedtime
\stopbuffer

\typebuffer[option=TEX]

An unexpanded loop is (of course) the fastest because it only collects and then
feeds back the lot. In an expanded loop each cycle does an expansion of the body
and collects the result which is then injected afterwards, and the controlled
loop just expands the body each iteration.

\startlines\tttf \getbuffer \stoplines

The different behavior is best illustrated with the following example:

\startbuffer[definition]
\edef\TestA{\localcontrolledloop 1 5 1 {A}} % out of order
\edef\TestB{\expandedloop        1 5 1 {B}}
\edef\TestC{\unexpandedloop      1 5 1 {C\relax}}
\stopbuffer

\typebuffer[definition][option=TEX]

We can show the effective definition:

\startbuffer[example]
\meaningasis\TestA
\meaningasis\TestB
\meaningasis\TestC

A: \TestA
B: \TestB
C: \TestC
\stopbuffer

\typebuffer[example][option=TEX]

Watch how the first test pushes the content in the main input stream:

\startlines\tttf \getbuffer[definition,example]\stoplines

Here are some examples that show what gets expanded and what not:

\startbuffer
\edef\whatever
  {\expandedloop 1 10 1
     {(\number\currentloopiterator)
      \scratchcounter=\number\currentloopiterator\relax}}

\meaningasis\whatever
\stopbuffer

\typebuffer[option=TEX]

\startpacked \veryraggedright \tt\tfx \getbuffer \stoppacked

A local control encapsulation hides the assignment:

\startbuffer
\edef\whatever
  {\expandedloop 1 10 1
     {(\number\currentloopiterator)
      \beginlocalcontrol
      \scratchcounter=\number\currentloopiterator\relax
      \endlocalcontrol}}

\meaningasis\whatever
\stopbuffer

\typebuffer[option=TEX]

\blank \start \veryraggedright \tt\tfx \getbuffer \stop \blank

Here we see the assignment being retained but with changing values:

\startbuffer
\edef\whatever
  {\unexpandedloop 1 10 1
     {\scratchcounter=1\relax}}

\meaningasis\whatever
\stopbuffer

\typebuffer[option=TEX]

\blank \start \veryraggedright \tt\tfx \getbuffer \stop \blank

We get no expansion at all:

\startbuffer
\edef\whatever
  {\unexpandedloop 1 10 1
     {\scratchcounter=\the\currentloopiterator\relax}}

\meaningasis\whatever
\stopbuffer

\typebuffer[option=TEX]

\blank \start \veryraggedright \tt\tfx \getbuffer \stop \blank

And here we have a mix:

\startbuffer
\edef\whatever
  {\expandedloop 1 10 1
     {\scratchcounter=\the\currentloopiterator\relax}}

\meaningasis\whatever
\stopbuffer

\typebuffer[option=TEX]

\blank \start \veryraggedright \tt\tfx \getbuffer \stop \blank

There is one feature worth noting. When you feed three numbers in a row, like here,
there is a danger of them being seen as one:

\starttyping
\expandedloop
  \number\dimexpr1pt
  \number\dimexpr2pt
  \number\dimexpr1pt
  {}
\stoptyping

This gives an error because a too large number is seen. Therefore, these loops
permit leading equal signs, as in assignments (we could support keywords but
it doesn't make much sense):

\starttyping
\expandedloop =\number\dimexpr1pt =\number\dimexpr2pt =\number\dimexpr1pt{}
\stoptyping

\stopsectionlevel

\startsectionlevel[title=Wrappers]

We always had loop helpers in \CONTEXT\ and the question is: \quotation {What we
will gain when we replace the definitions with ones using the above?}. The answer
is: \quotation {We have little performance but not as much as one expects!}. This
has to do with the fact that we support \type {#1} as iterator and \type {#2} as
(verbose) nesting values and that comes with some overhead. It is also the reason
why these loop macros are protected (unexpandable). However, using the primitives
might look somewhat more natural in low level \TEX\ code.

Also, replacing their definitions can have side effects because the primitives are
(and will be) still experimental so it's typically a patch that I will run on my
machine for a while.

Here is an example of two loops. The inner state variables have one hash, the outer
one extra:

\startbuffer
\dorecurse{2}{
    \dostepwiserecurse{1}{10}{2}{
        (#1:#2) [##1:##2]
    }\par
}
\stopbuffer

\typebuffer[option=TEX]

We get this:

\startpacked \getbuffer \stoppacked

We can also use two state macro but here we would have to store the outer ones:

\startbuffer
\dorecurse {2} {
    /\recursedepth:\recurselevel/
    \dostepwiserecurse {1} {10} {2} {
        <\recursedepth:\recurselevel>
    }\par
}
\stopbuffer

\typebuffer[option=TEX]

That gives us:

\startpacked \getbuffer \stoppacked

An endless loop works as follows:

\startbuffer
\doloop {
    ...
    \ifsomeconditionismet
        ...
        \exitloop
    \else
        ...
    \fi
  % \exitloopnow
    ...
}
\stopbuffer

\typebuffer[option=TEX]

Because of the way we quit there will not be a new implementation in terms of
the loop primitives. You need to make sure that you don't leave in the middle
of an ongoing condition. The second exit is immediate.

We also have a (simple) expanded variant:

\startbuffer
\edef\TestX{\doexpandedrecurse{10}{!}} \meaningasis\TestX
\stopbuffer

\typebuffer[option=TEX]

This helper can be implemented in terms of the loop primitives which makes them a
bit faster, but these are not critical:

\startpacked \getbuffer \stoppacked

A variant that supports \type {#1} is the following:

\startbuffer
\edef\TestX{\doexpandedrecursed{10}{#1}} \meaningasis\TestX
\stopbuffer

\typebuffer[option=TEX]

So:

\startpacked \getbuffer \stoppacked

% private: \dofastloopcs{#1}\cs with % \fastloopindex and \fastloopfinal

\stopsectionlevel

\stopdocument