Repeat: Difference between revisions
Content added Content deleted
No edit summary |
(Add Nim implementation (Python highlighting because there's no Nim highlighting)) |
||
Line 1,019: | Line 1,019: | ||
repeat(procedure, 3)</lang> |
repeat(procedure, 3)</lang> |
||
=={{header|Nim}}== |
|||
<lang python>proc example = |
|||
echo "Example" |
|||
# Ordinary procedure |
|||
proc repeatProc(fn: proc, n: int) = |
|||
for x in 0..<n: |
|||
fn() |
|||
repeatProc(example, 4) |
|||
# Template (code substitution), simplest form of metaprogramming |
|||
# that Nim has |
|||
template repeatTmpl(n: int, body: untyped): untyped = |
|||
for x in 0..<n: |
|||
body |
|||
# This gets rewritten into a for loop |
|||
repeatTmpl 4: |
|||
example() |
|||
import std/macros |
|||
# A macro which takes some code block and returns code |
|||
# with that code block repeated n times. Macros run at |
|||
# compile-time |
|||
macro repeatMacro(n: static[int], body: untyped): untyped = |
|||
result = newStmtList() |
|||
for x in 0..<n: |
|||
result.add body |
|||
# This gets rewritten into 4 calls to example() |
|||
# at compile-time |
|||
repeatMacro 4: |
|||
example() |
|||
</lang> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |