sourceuserlibs/Repeat.xtlm
1⍝# A macro library of your own (userlibs/, found as libraries of your
2⍝# own are): statements written as many times as you ask, when the
3⍝# program is expanded, a loop unrolled at compile time. Import it with
4⍝# an alias of your choice, "r:" u_se< "Repeat", and see what a program
5⍝# becomes with xetal expand FILE.
6
7⍝## Repeating statements
8
9⍝# The statements on the right, written the number of times on the
10⍝# left, one copy per line; it stands as a statement of its own. A
11⍝# count below 1 is refused when the program is expanded.
12⍝# >> "r:" u_se< "Repeat"
13⍝# >> "3" r:t_imes< "\"hip hip\""
14⍝# hip hip
15⍝# hip hip
16⍝# hip hip
17ᵐt̲imes< ← { count body →
18 n̲ot ⎕S̲TATEMENT @ ? "misplaced-macro call" ⎕R̲EJECT "r:t_imes< writes statements: it stands as a statement of its own"
19 k ← f̲loor f̲irst (n̲umbers count) c̲at 0.0
20 k < 1 ? "bad-macro-argument left" ⎕R̲EJECT "r:t_imes< writes its statements 1 or more times"
21 k ʰc̲opies body
22}
23
24⍝# The text t written k times, one copy per line (once when k is 1 or
25⍝# less).
26ʰc̲opies ← { k t → k ≤ 1 ? t◆ t c̲at "\n" c̲at (k − 1) ʰc̲opies t }