Earlier quoted context omitted.
Fun fact- Nimrod means mighty hunter. The negative connotation came from early Bugs Bunny cartoons where he applies the name sarcastically to Elmer Fudd.
Kind of. https://en.wikipedia.org/wiki/Nimrod There's definitely a much older negative connotation than the Bugs Bunny usage.
A cursory look at meta-programming in Nim
11–20 of 30 posts
Re: A cursory look at meta-programming in Nim
#12Way too cumbersome, sorry. 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))
Re: A cursory look at meta-programming in Nim
#13Way too cumbersome, sorry. 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])Re: A cursory look at meta-programming in Nim
#14Way too cumbersome, sorry. 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))
Yes but on the plus side there's a lot fewer parentheses.
wait - oops. :)
Re: A cursory look at meta-programming in Nim
#15I think the author should post a follow-up after he's tried to do substantial work with this style meta-programming. My guess is that you want to keep templated code to an absolute minimum.
Re: A cursory look at meta-programming in Nim
#16 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])Re: A cursory look at meta-programming in Nim
#17Or, with a text based meta-layer (MyDef): 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 Re: A cursory look at meta-programming in Nim
#18Or, with a text based meta-layer (MyDef): 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])
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]);Re: A cursory look at meta-programming in Nim
#19Way too cumbersome, sorry. 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))
Yes but on the plus side there's a lot fewer parentheses.
Re: A cursory look at meta-programming in Nim
#20Or, with a text based meta-layer (MyDef): 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])
You might as well use the POSIX shell to write your code: cat
Now how do I incorporate this into my tool chain (Makefile)?