Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6647201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:30:34+00:00 2026-05-26T00:30:34+00:00

I have a bunch of functions that map to and from some codes defined

  • 0

I have a bunch of functions that map to and from some codes defined by an external system:

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

I’d like to make a macro to remove the duplication:

#_ (translation-table size-tick-field-code
                      {:bid-size 0
                       :ask-size 3
                       :last-size 5
                       :volume 8})    

I started the macro like this:

(defmacro translation-table [name & vals]
  `(defn ~(symbol (str "translate-to-ib-" name)) [val#]
     (get ~@vals val#)))

The resulting function body seems right, but the function name is wrong:

re-actor.conversions> (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 ...

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.

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:30:34+00:00Added an answer on May 26, 2026 at 12:30 am

    The macro issue is twofold:

    1) You’re using a backtick when quoting the form passed to macroexpand, which namespace-qualifies the symbols within:

    `(translation-table monkey {:a 1 :b 2})
    => (foo.bar/translation-table foo.bar/monkey {:a 1, :b 2})
    

    where foo.bar is whatever namespace you’re in.

    2) You’re constructing the name of the defn item using the symbol name, which, when it is namespace-qualified, will stringify to “foo.bar/monkey”. Here’s a version that will work:

    (defmacro translation-table [tname & vals]
      `(defn ~(symbol (str "translate-to-ib-" (name tname))) [val#]
         (get ~@vals val#)))
    

    Notice that we’re getting the name of tname without the namespace part, using the name function.

    As for whether a macro is the right solution here, probably not 🙂 For a simple case like this, I might just use maps:

    (def translate-from-ib-size-tick-field-code 
      {0 :bid-size
       3 :ask-size
       5 :last-size
       8 :volume})
    
    ;; swap keys & vals
    (def translate-to-ib-size-tick-field-code
      (zipmap (vals translate-from-ib-size-tick-field-code)
              (keys translate-from-ib-size-tick-field-code)))
    
    (translate-from-ib-size-tick-field-code 0)
    => :bid-size
    
    (translate-to-ib-size-tick-field-code :bid-size)
    => 0
    

    If speed is of the essence, check out case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of user-defined functions that are frequently used in mathematica. I
I have a bunch of functions that I created in some views that must
I have a bunch of overloaded functions that operate on certain data types such
I have a bunch of generated functions from a supplier's tool are required to
I have a bunch of javascript functions which depend on some server-side constants (such
Summary: I have a bunch of C functions I have to call from C#.
So I want to have a bunch of functions that receive a state and
I am working on a project where I have a bunch of functions that
I have a bunch of Javascript functions that look like the following: function generateBusinessImage
I have a map with a bunch of buttons that show and hide container

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.