variables - Clojure - What can I do to correctly use Vars and/or Atoms? -
i'm having lot of trouble clojure since i'm new it. i'm attempting solve orthanormal vector matrix [[i, j, k], [5, 2, 3], [8, 3, 2]]. know algorithm is, i'm confused when use atoms , vars. result, continue errors. point out i'm doing wrong , how can improve code?
listed below code error.
(defn solve [& rows] (let [orthanormal-vector (atom [])] (let [dimension-range (range (count rows)) determinant-argument (atom [])] (doseq [[i row] (map list dimension-range rows)] (let [row-range (range (count row)) determinant-vector (atom [])] (doseq [[j e] (map list row-range (seq row))] (if-not (= j) (swap! determinant-vector (conj determinant-vector e)) (println "do nothing"))) (var-set determinant-argument (conj determinant-argument determinant-vector))) (def determinant-result (incanter-core/det (incanter-core/matrix (vec determinant-argument)))) (swap! orthanormal-vector (conj orthanormal-vector determinant-result)) ))))
java.lang.classcastexception: clojure.lang.atom cannot cast clojure.lang.ipersistentcollection core.clj:83 clojure.core/conj /users/chriskim/desktop/promethix/src/promethix/orthanormal/solver.clj:14 promethix.orthanormal.solver/solve restfn.java:436 clojure.lang.restfn.invoke /users/chriskim/desktop/promethix/src/promethix/orthanormal/solver.clj:24 promethix.orthanormal.solver/eval16321 compiler.java:6703 clojure.lang.compiler.eval compiler.java:6666 clojure.lang.compiler.eval core.clj:2927 clojure.core/eval eval.clj:77 lighttable.nrepl.eval/->result afn.java:156 clojure.lang.afn.applytohelper afn.java:144 clojure.lang.afn.applyto core.clj:626 clojure.core/apply core.clj:2468 clojure.core/partial[fn] restfn.java:408 clojure.lang.restfn.invoke core.clj:2559 clojure.core/map[fn] lazyseq.java:40 clojure.lang.lazyseq.sval lazyseq.java:49 clojure.lang.lazyseq.seq rt.java:484 clojure.lang.rt.seq core.clj:133 clojure.core/seq core.clj:2595 clojure.core/filter[fn] lazyseq.java:40 clojure.lang.lazyseq.sval lazyseq.java:49 clojure.lang.lazyseq.seq cons.java:39 clojure.lang.cons.next rt.java:598 clojure.lang.rt.next core.clj:64 clojure.core/next core.clj:2856 clojure.core/dorun core.clj:2871 clojure.core/doall eval.clj:126 lighttable.nrepl.eval/eval-clj restfn.java:442 clojure.lang.restfn.invoke eval.clj:192 lighttable.nrepl.eval/eval10834[fn] afn.java:152 clojure.lang.afn.applytohelper afn.java:144 clojure.lang.afn.applyto core.clj:624 clojure.core/apply core.clj:1862 clojure.core/with-bindings* restfn.java:425 clojure.lang.restfn.invoke eval.clj:177 lighttable.nrepl.eval/eval10834[fn] eval.clj:176 lighttable.nrepl.eval/eval10834[fn] multifn.java:227 clojure.lang.multifn.invoke core.clj:98 lighttable.nrepl.core/queued[fn] core.clj:2402 clojure.core/comp[fn] interruptible_eval.clj:138 clojure.tools.nrepl.middleware.interruptible-eval/run-next[fn] afn.java:22 clojure.lang.afn.run threadpoolexecutor.java:895 java.util.concurrent.threadpoolexecutor$worker.runtask threadpoolexecutor.java:918 java.util.concurrent.threadpoolexecutor$worker.run thread.java:680 java.lang.thread.run
the specific error resulting in clojure.lang.atom cannot cast clojure.lang.ipersistentcollection
is:
(swap! determinant-vector (conj determinant-vector e))
shoudl written:
(swap! determinant-vector conj e)
swap! takes atom it's first argument , function second argument. builds function call passing current value in atom first argument function , appends rest of arguments after that. translated function call this:
(conj @determinant-vector e)
ps: @ reader macro calling deref
current value out of 1 of mutable state types.
pps: charles duffy correct loop/recur much better tools task using atom. should 100x faster, @ least in terms of overhead.
Comments
Post a Comment