Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Everything in this function is deterministic and pure (except the <code>info</code> calls, which shouldn't matter), so it should return the same thing every time. You don't provide any sample inputs, so I can't disprove this assumption.</p> <p>The code is hard to read, and with no context I don't really understand what you're doing. But I went through your code in several refactoring passes to try to make it clearer what's going on. Hopefully this will help someone else who is reading, or even make it clearer to you where your problem is.</p> <h2>First</h2> <p>Remove all the crazy formatting and pointless variable-copying, and use <code>seq</code> instead of testing count=0</p> <pre><code>(defn resolve-case-per-period "Constructs a map by matching data existing in input parameter vectors" [dd case-details periods] (let [cases (loop [case-details case-details, result-map {}] (if (seq case-details) (recur (rest case-details) (assoc result-map (:priority (first case-details)) (:caseid (first case-details)))) result-map))] (info "Mapping cases to periods step 1 completed " cases) (loop [periods periods, result-map {}] (if (seq periods) (recur (rest periods) (assoc result-map (str (:period (first periods))) (get cases (:priority (first periods))))) (do (info "Mapping cases to periods step 2 completed " result-map) result-map))))) </code></pre> <h2>Second</h2> <p>Destructuring and if-let instead of primitive keyword lookups, ifs, and seqs:</p> <pre><code>(defn resolve-case-per-period "Constructs a map by matching data existing in input parameter vectors" [dd case-details periods] (let [cases (loop [case-details case-details, result-map {}] (if-let [[{:keys [priority case-id]} &amp; more] (seq case-details)] (recur more (assoc result-map priority caseid)) result-map))] (info "Mapping cases to periods step 1 completed " cases) (loop [periods periods, result-map {}] (if-let [[{:keys [period priority]} &amp; more] (seq periods)] (recur more (assoc result-map (str period) (get cases priority))) (do (info "Mapping cases to periods step 2 completed " result-map) result-map))))) </code></pre> <h2>Third</h2> <p>At this point it's finally clear that we're just iterating over a sequence and building up a result value as we go, so we can drop all the first/rest nonsense and just use <code>reduce</code> to traverse the sequence for us:</p> <pre><code>(defn resolve-case-per-period "Constructs a map by matching data existing in input parameter vectors" [dd case-details periods] (let [cases (reduce (fn [result-map {:keys [priority case-id]}] (assoc result-map priority caseid)) {}, case-details)] (info "Mapping cases to periods step 1 completed " cases) (reduce (fn [result-map {:keys [period priority]}] (assoc result-map (str period) (get cases priority))) {}, periods))) </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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.
 

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