Live data from Hacker News

A cursory look at meta-programming in Nim

blog.ldlework.com

11–20 of 30 posts

Re: A cursory look at meta-programming in Nim

#11
post #6
post #4

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.

You don't like big waterfall projects like the Tower of Babel? You must be one of those microservices people... b^)

Re: A cursory look at meta-programming in Nim

#12

Way 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

#13

Way 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))

You can do it a bit shorter and easier in Nim as well:

    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

#14
post #12

Way 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.

You were down-voted but I secretly agree.

wait - oops. :)

Re: A cursory look at meta-programming in Nim

#15
post #9

I 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.

You were down-voted but I actually agree. I did conclude the article by saying that templates seem really nice for closing the gap on refactoring code that not even generics could wrap up. So the intention was to convey that templates are really a last resort secret weapon. They just also seem to be pretty easy to understand :)

Re: A cursory look at meta-programming in Nim

#17

Or, 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 

Re: A cursory look at meta-programming in Nim

#18

Or, 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])

Hey i can do that in $php too!

    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

#19
post #12

Way 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.

If you're counting tokens, you should count all of them: all symbols and punctuation, as well as any whitespace outside of a string literal that cannot be replaced by a single space character without affecting the syntax.

Re: A cursory look at meta-programming in Nim

#20

Or, 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

It worked!

Now how do I incorporate this into my tool chain (Makefile)?

Post reply on HN