Note that there are some explanatory texts on larger screens.

plurals
  1. POMake a namespace qualified function name in a macro
    text
    copied!<p>I have a bunch of functions that map to and from some codes defined by an external system:</p> <pre><code>(defn translate-from-ib-size-tick-field-code [val] (condp = val 0 :bid-size 3 :ask-size 5 :last-size 8 :volume)) (defn translate-to-ib-size-tick-field-code [val] (condp = val :bid-size 0 :ask-size 3 :last-size 5 :volume 8)) </code></pre> <p>I'd like to make a macro to remove the duplication:</p> <pre><code>#_ (translation-table size-tick-field-code {:bid-size 0 :ask-size 3 :last-size 5 :volume 8}) </code></pre> <p>I started the macro like this:</p> <pre><code>(defmacro translation-table [name &amp; vals] `(defn ~(symbol (str "translate-to-ib-" name)) [val#] (get ~@vals val#))) </code></pre> <p>The resulting function body seems right, but the function name is wrong:</p> <pre><code>re-actor.conversions&gt; (macroexpand `(translation-table monkey {:a 1 :b 2})) (def translate-to-ib-re-actor.conversions/monkey (.withMeta (clojure.core/fn translate-to-ib-re-actor.conversions/monkey ([val__10589__auto__] (clojure.core/get {:a 1, :b 2} val__10589__auto__))) (.meta ... </code></pre> <p>I'd like the "translate-to-ib-" to appear as part of the function name, instead of a prefix to the namespace, as it turned out.</p> <p>How can I do this with clojure macros? If I am just doing it wrong and shouldn't use macros for this for some reason, please do let me know, but I would also like to know how to create function names like this to just improve my understanding of clojure and macros in general. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload