Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I do not have a solution for your problem, but I can't resist suggesting some improvement on the first two functions:</p> <pre><code>(defn extend-regs [reg s o] (let [os (get reg s)] (if (nil? os) (assoc reg s [o]) (assoc reg s (conj os o))))) ;; =&gt; place the 'if inside the assoc: (defn extend-regs [reg s o] (let [os (get reg s)] (assoc reg s (if (nil? os) [o] (conj os o))))) ;; =&gt; this (if (nil? x) ...) is the pattern of function fnil, so ... (defn extend-regs [reg s o] (let [os (get reg s)] (assoc reg s ((fnil conj []) os o)))) ;; with update-in, this will be even clearer, and we can remove the let altogether: (defn extend-regs [reg s o] (update-in reg [s] (fnil conj []) o)) </code></pre> <p>As for the second:</p> <pre><code>(defn add-reg! [d s o] (send d (fn [a] (assoc a :regs (extend-regs (:regs a) s o))))) ;; =&gt; We can, again, use update-in instead of assoc: (defn add-reg! [d s o] (send d (fn [a] (update-in a [:regs] extend-regs s o)))) ;; or, if you can get rid of extend-regs: (defn add-reg! [d s o] (send d (fn [a] (update-in a [:regs s] (fnil conj []) o))) </code></pre> <p>Finally, as a matter of style, I would place add-reg in a separate function, and directly use the idiom of sending to the agent in the client code (or have a simplified add-reg! function):</p> <pre><code>(defn add-reg [v s o] (update-in v [:regs s] (fnil conj []) o)) (defn add-reg! [d s o] (send d add-reg)) </code></pre> <p>I know this doesn't answer your initial question, but it was fun to write this step by step refactoring</p>
 

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