Technically this is just function call using method call syntax - ×
is treated as regular identifier (like someFunction
) and it is perfectly legal to call obj .someFunction arg
. So a .× b
is not really different in that regard.
proc ×(a, b: set[char]): seq[(char, char)] =
for aIt in a:
for bIt in b:
result.add (aIt, bIt)
echo {'c', 'd'} .× {'a', 'b'}
@[('c', 'a'), ('c', 'b'), ('d', 'a'), ('d', 'b')]
let hello = 12
echo hE_LL_o
12
import macros
dumpTree:
2 .× ⟦ {t₁ .∈ s(t₁) | (t₁, t₂) .∈ M} ⟧
---------------------------------------
⟦ s(t₂) ⟧ + ⟦ s(t₁) ⟧
echo "compile ok!"
compile ok!
proc tmp(a = 12, b = a + 2, c = ((
block:
var buf: string
for i in 0 .. 2:
buf &= " * " & $i
buf
))
) = echo b, c
tmp(b = 122)
122 * 0 * 1 * 2
#?replace(sub="function", by="func")
function nice(x, y: int): int = x + y
echo 12 .nice 50
62
import macros
proc arrImpl(a, b, c: int): void = echo a, " ", b, " ", c
macro `~`(lhs: untyped{nkBracketExpr}, c: int): untyped =
let a = lhs[0]
let b = lhs[1]
quote do:
arrImpl(`a`, `b`, `c`)
2[3] ~ 4
2 3 4
Use { `|` * a }
pattern for term rewriting template to collect all arguments into single list and then pass to actual implementation.
proc `|`(s: varargs[int]): int =
for arg in s:
result += arg
proc `|`(a: int, b: string): string =
$a & "--" & b
template optPipe{ `|` * a }(a: int): untyped = |a
echo 2 | 3 | (12 + 23) | "hello"
40--hello