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

  • SEARCH
  • Home
  • 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 3680736
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:36:41+00:00 2026-05-19T03:36:41+00:00

I want to send var-args of a function to a macro, still as var-args.

  • 0

I want to send var-args of a function to a macro, still as var-args.
Here is my code:

(defmacro test-macro
 [& args]
 `(println (str "count=" ~(count args) "; args=" ~@args)))

(defn test-fn-calling-macro
 [& args]
 (test-macro args))

The output of (test-macro "a" "b" "c") is what I want: count=3; args=abc

The output of (test-fn-calling-macro "a" "b" "c") is : count=1; args=("a" "b" "c") because args is sent as a single argument to the macro. How can I expand this args in my function in order to call the macro with the 3 arguments?

I guess I’m just missing a simple core function but I’m not able to find it. Thanks


EDIT 2 – My “real” code, shown in EDIT section below is not a valid situation to use this technique.

As pointed out by @Brian, the macro xml-to-cass can be replaced with a function like this:

(defn xml-to-cass
  [zipper table key attr & path]
  (doseq [v (apply zf/xml-> zipper path)] (cass/set-attr! table key attr v)))

EDIT – the following section goes beyond my original question but any insight is welcome

The code above is just the most simple I could come with to pinpoint my problem. My real code deals with clj-cassandra and zip-filter. It may also look over-engineering but it’s just a toy project and I’m trying to learn the language at the same time.

I want to parse some XML found on mlb.com and insert values found into a cassandra database. Here is my code and the thinking behind it.

Step 1 – Function which works fine but contains code duplication

(ns stats.importer
  (:require
    [clojure.xml :as xml]
    [clojure.zip :as zip]
    [clojure.contrib.zip-filter.xml :as zf]
    [cassandra.client :as cass]))

(def root-url "http://gd2.mlb.com/components/game/mlb/year_2010/month_05/day_01/")

(def games-table (cass/mk-cf-spec "localhost" 9160 "mlb-stats" "games"))

(defn import-game-xml-1
  "Import the content of xml into cassandra"
  [game-dir]
  (let [url (str root-url game-dir "game.xml")
        zipper (zip/xml-zip (xml/parse url))
        game-id (.substring game-dir 4 (- (.length game-dir) 1))]
    (doseq [v (zf/xml-> zipper (zf/attr :type))] (cass/set-attr! games-table game-id :type v))
    (doseq [v (zf/xml-> zipper (zf/attr :local_game_time))] (cass/set-attr! games-table game-id :local_game_time v))
    (doseq [v (zf/xml-> zipper :team [(zf/attr= :type "home")] (zf/attr :name_full))] (cass/set-attr! games-table game-id :home_team v))))

The parameter to import-game-xml-1 can be for example "gid_2010_05_01_colmlb_sfnmlb_1/". I remove the “gid_” and the trailing slash to make it the key of the ColumnFamily games in my database.

I found that the 3 doseq were a lot of duplication (and there should be more than 3 in the final version). So code templating using a macro seemed appropriate here (correct me if I’m wrong).

Step 2 – Introducing a macro for code templating (still works)

(defmacro xml-to-cass
  [zipper table key attr & path]
  `(doseq [v# (zf/xml-> ~zipper ~@path)] (cass/set-attr! ~table ~key ~attr v#)))

(defn import-game-xml-2
  "Import the content of xml into cassandra"
  [game-dir]
  (let [url (str root-url game-dir "game.xml")
        zipper (zip/xml-zip (xml/parse url))
        game-id (.substring game-dir 4 (- (.length game-dir) 1))]
    (xml-to-cass zipper games-table game-id :type (zf/attr :type))
    (xml-to-cass zipper games-table game-id :local_game_time (zf/attr :local_game_time))
    (xml-to-cass zipper games-table game-id :home_team :team [(zf/attr= :type "home")] (zf/attr :name_full))))

I believe that’s an improvement but I still see some duplication in always reusing the same 3 parameters in my calls to xml-to-cass. That’s were I introduced an intermediate function to take care of those.

Step 3 – Adding a function to call the macro (the problem is here)

(defn import-game-xml-3
  "Import the content of xml into cassandra"
  [game-dir]
  (let [url (str root-url game-dir "game.xml")
        zipper (zip/xml-zip (xml/parse url))
        game-id (.substring game-dir 4 (- (.length game-dir) 1))
        save-game-attr (fn[key path] (xml-to-cass zipper games-table game-id key path))]
    (save-game-attr :type (zf/attr :type)) ; works well because path has only one element
    (save-game-attr :local_game_time (zf/attr :local_game_time))
    (save-game-attr :home :team [(zf/attr= :type "home"] (zf/attr :name_full))))) ; FIXME this final line doesn't work
  • 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-19T03:36:42+00:00Added an answer on May 19, 2026 at 3:36 am

    Here’s a some simple code which may be illuminating.

    Macros are about code generation. If you want that to happen at runtime, for some reason, then you have to build and evaluate the code at runtime. This can be a powerful technique.

    (defmacro test-macro
     [& args]
     `(println (str "count=" ~(count args) "; args=" ~@args)))
    
    (defn test-fn-calling-macro
     [& args]
     (test-macro args))
    
    (defn test-fn-expanding-macro-at-runtime
      [& args]
      (eval (cons `test-macro args)))
    
    (defmacro test-macro-expanding-macro-at-compile-time
      [& args]
      (cons `test-macro args))
    
    ;; using the splicing notation
    
    (defmacro test-macro-expanding-macro-at-compile-time-2
      [& args]
      `(test-macro ~@args))
    
    (defn test-fn-expanding-macro-at-runtime-2
      [& args]
      (eval `(test-macro ~@args)))
    
    
    
    (test-macro "a" "b" "c") ;; count=3; args=abc nil
    (test-fn-calling-macro "a" "b" "c") ;; count=1; args=("a" "b" "c") nil
    
    (test-fn-expanding-macro-at-runtime "a" "b" "c") ; count=3; args=abc nil
    (test-macro-expanding-macro-at-compile-time "a" "b" "c") ; count=3; args=abc nil
    (test-macro-expanding-macro-at-compile-time-2 "a" "b" "c") ; count=3; args=abc nil
    (test-fn-expanding-macro-at-runtime "a" "b" "c") ; count=3; args=abc nil
    

    If contemplation of the above doesn’t prove enlightening, might I suggest a couple of my own blog articles?

    In this one I go through macros from scratch, and how clojure’s work in particular:

    http://www.learningclojure.com/2010/09/clojure-macro-tutorial-part-i-getting.html

    And in this one I show why run-time code generation might be useful:

    http://www.learningclojure.com/2010/09/clojure-faster-than-machine-code.html

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

Sidebar

Related Questions

I want to send array to my action method: var items = { 'myIdList[]':
I have the following array: var idParam = [1,2,3]; I want to send this
I have the code below. I want to send the value of value1 n.value1s
I am using this code to send email var message = new MailMessage(abc@somedomain.com, administrator@anotherdomain.com);
I want to send a string data with jquery load function but its not
I want to send geographic data (latitude & longitude) to a mySQL database with
How to get token in a function ? I want send it out of
I am using the following code to hook into XMLHttpRequest open/send methods: var lastParams
I want to send json formatted data to jQuery.css() function, and i dont know
If I want to send an object to a function with one child called

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.