Clojure: Pass 'expanded' optional args to function

arguments, clojure, keyword, option-type, sequence

Solution

I think you are looking for apply (http://clojuredocs.org/clojure_core/clojure.core/apply)

(defn get-node-value [parsed-xml & node-path]
  (let [params (concat node-path [zd/text])]
    (apply zd/xml-> (zip/xml-zip parsed-xml) params)))

Problem

I'm new to Clojure and I'm stuck on how to 'expand' a function's optional args so they can be sent to another function that uses optional args (but wants those args as keywords not a seq of keywords). I'm parsing xml and if I hard code values as below my function works, it walks the xml and finds the value of 'title': ``` ; zd was required like this [clojure.data.zip.xml :as zd] ; ... (defn get-node-value [parsed-xml & node-path] (zd/xml-> (zip/xml-zip parsed-xml) :item :title zd/text)) (get-node-value parsed-xml) ``` What I want to do is use 'node-path' to pass in any number of keywords, but when written as below it comes in as a sequence of keywords so it throws an exception: ``` (defn get-node-value [parsed-xml & node-path] (zd/xml-> (zip/xml-zip parsed-xml) node-path zd/text)) (get-node-value parsed-xml :item :title) ; ClassCastException clojure.lang.ArraySeq cannot be cast to clojure.lang.IFn clojure.data.zip/fixup-apply (zip.clj:73) ``` thanks!

Original source