Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The arrow macro (->) just rewrites its arguments so that the value of the nth form is inserted as the first argument to the n+1th form. What you are writing is equivalent to:</p> <pre><code>(case (.compile (.newXPath (XPathFactory/newInstance)) xpath) return-type :node-list (.evaluate document XPathConstants/NODESET) :node (.evaluate document XPathConstants/NODE) :number (.evaluate document XPathConstants/NUMBER) </code></pre> <p>In a general case you can pick one of the three forms to be your tail form ahead of time using <code>let</code>, and then thread that in at the end of the threading macro. Like so:</p> <pre><code>(defn eval-xpath [document xpath return-type] (let [evaluator (case return-type :node-list #(.evaluate % document XPathConstants/NODESET) :node #(.evaluate % document XPathConstants/NODE) :number #(.evaluate % document XPathConstants/NUMBER))] (-&gt; (XPathFactory/newInstance) .newXPath (.compile xpath) (evaluator)))) </code></pre> <p>However what you are really trying to do is map a keyword to a constant on XPathConstants. This can be done with a map. Consider the following: </p> <pre><code>(defn eval-xpath [document xpath return-type] (let [constants-mapping {:node-list XPathConstants/NODESET :node XPathConstants/NODE :number XPathConstants/NUMBER}] (-&gt; (XPathFactory/newInstance) .newXPath (.compile xpath) (.evaluate document (constants-mapping return-type))))) </code></pre> <p>You have a mapping of keywords to constants, so use Clojure's data structures to express that. Additionally the real value of the threading macro is helping you compile the xpath. Don't be afraid to give the data you're using locally scoped names to help you keep track of what you are doing. It also helps you avoid trying to shoehorn things into the threading macro that really don't want to fit.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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