macro libraryuserlibs/Repeat.xtlm

A macro library of your own (userlibs/, found as libraries of your own are): statements written as many times as you ask, when the program is expanded, a loop unrolled at compile time. Import it with an alias of your choice, "r:" u_se< "Repeat", and see what a program becomes with xetal expand FILE.

source

Repeating statements

ᵐt̲imes< : Char -> Char -> Char

macro · line 17

The statements on the right, written the number of times on the left, one copy per line; it stands as a statement of its own. A count below 1 is refused when the program is expanded.

      ʳ⁼u̲se< "Repeat"
      "3" ʳt̲imes< "\"hip hip\""
hip hip
hip hip
hip hip
ᵐt̲imes< ← { count body →
  n̲ot ⎕S̲TATEMENT @ ? "misplaced-macro call" ⎕R̲EJECT "r:t_imes< writes statements: it stands as a statement of its own"
  k ← f̲loor f̲irst (n̲umbers count) c̲at 0.0
  k < 1 ? "bad-macro-argument left" ⎕R̲EJECT "r:t_imes< writes its statements 1 or more times"
  k ʰc̲opies body
}

ʰc̲opies : Int -> Char -> Char

function (private) · line 26

The text t written k times, one copy per line (once when k is 1 or less).

ʰc̲opies ← { k t → k ≤ 1 ? t◆ t c̲at "\n" c̲at (k − 1) ʰc̲opies t }