import macros
$(for:A in A,B,C)
proc $(A)() = echo "$(A)"
proc execute(order: seq[int], callbacks: seq[proc]) =
for i in items(order):
callbacks[i]()
execute(@[0,0,1,2,1,2], @[A, B, C]) cat <<END
$(for x in A B C; do
echo "proc $x() = echo \"$x\"";
done)
proc execute(order: seq[int], callbacks: seq[proc]) =
for i in items(order):
callbacks[i]()
execute(@[0,0,1,2,1,2], @[A, B, C])
END function execute($order, $callbacks){
foreach($order as $i){
$callbacks[$i]();
}
}
function abcProcs(){
foreach(func_get_args() as $f){
eval("function $f() { echo '$f'; };");
}
}
abcProcs("A","B","C");
execute([0,0,1,2,1,2], ['A','B','C']);
edit:you don't have to quote the function names (just to show they are really functions and not closures):
execute([0,0,1,2,1,2], [A,B,C]);Nim isn't C/C++, but it performs like it is C/C++. Nim isn't Golang, either, but it compiles programs like Go does, down to a self-contained binary with no dependencies. You can even statically link a Nim program against musl libc to ship without even a dependency on glibc.
This puts a completely new spin on writing performant code - just skip the C and write your code in pure Nim. It's a lot like writing Python except you produce comparable results to what you would get from writing your code in C or Go.
TXR Lisp:
@(do
(macro-time
(defun abc-proc (n)
^(defun ,n () (pprinl ',n))))
(defmacro abc-procs (. n)
^(progn ,*[mapcar abc-proc n]))
(abc-procs a b c)
(defun exec (order callbacks)
(each ((i order))
[[callbacks i]]))
(exec '(0 0 1 2 1 2) '(a b c)))
$ txr test.txr
a
a
b
c
b
c
Variation on exec: (defun exec (order callbacks)
(mapdo (op [callbacks @1]) order)) import macros, strutils
macro abcProcs(n: varargs[expr]): stmt =
result = newStmtList()
for c in n.children:
result.add parseStmt("proc $1 = echo \"$1\"" % $c)
abcProcs("A","B","C")
proc execute(order: openarray[int], callbacks: openarray[proc]) =
for i in order:
callbacks[i]()
execute([0,0,1,2,1,2], [A,B,C])wait - oops. :)
There's definitely a much older negative connotation than the Bugs Bunny usage.
I need to have another look at Nim. It even looks like it's in Debian!