Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
34
Task/Repeat/Nim/repeat.nim
Normal file
34
Task/Repeat/Nim/repeat.nim
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue