Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
11
Task/Metaprogramming/Nim/metaprogramming-1.nim
Normal file
11
Task/Metaprogramming/Nim/metaprogramming-1.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
proc `^`*[T: SomeInteger](base, exp: T): T =
|
||||
var (base, exp) = (base, exp)
|
||||
result = 1
|
||||
|
||||
while exp != 0:
|
||||
if (exp and 1) != 0:
|
||||
result *= base
|
||||
exp = exp shr 1
|
||||
base *= base
|
||||
|
||||
echo 2 ^ 10 # 1024
|
||||
6
Task/Metaprogramming/Nim/metaprogramming-2.nim
Normal file
6
Task/Metaprogramming/Nim/metaprogramming-2.nim
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
when defined windows:
|
||||
echo "Call some Windows specific functions here"
|
||||
elif defined linux:
|
||||
echo "Call some Linux specific functions here"
|
||||
else:
|
||||
echo "Code for the other platforms"
|
||||
2
Task/Metaprogramming/Nim/metaprogramming-3.nim
Normal file
2
Task/Metaprogramming/Nim/metaprogramming-3.nim
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
static:
|
||||
echo "Hello Compile time world: ", 2 ^ 10
|
||||
1
Task/Metaprogramming/Nim/metaprogramming-4.nim
Normal file
1
Task/Metaprogramming/Nim/metaprogramming-4.nim
Normal file
|
|
@ -0,0 +1 @@
|
|||
const x = 2 ^ 10
|
||||
14
Task/Metaprogramming/Nim/metaprogramming-5.nim
Normal file
14
Task/Metaprogramming/Nim/metaprogramming-5.nim
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import os
|
||||
|
||||
const debug = false
|
||||
|
||||
proc expensive: string =
|
||||
sleep(milsecs = 100)
|
||||
result = "That was difficult"
|
||||
|
||||
proc log(msg: string) =
|
||||
if debug:
|
||||
echo msg
|
||||
|
||||
for i in 1..10:
|
||||
log expensive()
|
||||
6
Task/Metaprogramming/Nim/metaprogramming-6.nim
Normal file
6
Task/Metaprogramming/Nim/metaprogramming-6.nim
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
template log(msg: string) =
|
||||
if debug:
|
||||
echo msg
|
||||
|
||||
for i in 1..10:
|
||||
log expensive()
|
||||
7
Task/Metaprogramming/Nim/metaprogramming-7.nim
Normal file
7
Task/Metaprogramming/Nim/metaprogramming-7.nim
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
template times(x, y: untyped): untyped =
|
||||
for i in 1..x:
|
||||
y
|
||||
|
||||
10.times: # or times 10: or times(10):
|
||||
echo "hi"
|
||||
echo "bye"
|
||||
15
Task/Metaprogramming/Nim/metaprogramming-8.nim
Normal file
15
Task/Metaprogramming/Nim/metaprogramming-8.nim
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
template optLog1{a and a}(a): auto = a
|
||||
template optLog2{a and (b or (not b))}(a,b): auto = a
|
||||
template optLog3{a and not a}(a: int): auto = 0
|
||||
|
||||
var
|
||||
x = 12
|
||||
s = x and x
|
||||
# Hint: optLog1(x) --> ’x’ [Pattern]
|
||||
|
||||
r = (x and x) and ((s or s) or (not (s or s)))
|
||||
# Hint: optLog2(x and x, s or s) --> ’x and x’ [Pattern]
|
||||
# Hint: optLog1(x) --> ’x’ [Pattern]
|
||||
|
||||
q = (s and not x) and not (s and not x)
|
||||
# Hint: optLog3(s and not x) --> ’0’ [Pattern]
|
||||
13
Task/Metaprogramming/Nim/metaprogramming-9.nim
Normal file
13
Task/Metaprogramming/Nim/metaprogramming-9.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import macros
|
||||
|
||||
dumpTree:
|
||||
if x:
|
||||
if y:
|
||||
p0
|
||||
else:
|
||||
p1
|
||||
else:
|
||||
if y:
|
||||
p2
|
||||
else:
|
||||
p3
|
||||
Loading…
Add table
Add a link
Reference in a new issue