Reputation: 47
i'm new to clojure and functional programming so i would appreciate some help. :)
What i'm trying to do is : I'm trying to get this [1 1 2] from this "structure" [[1 0 1] [0 1 0] [0 0 1]] therefore i'm trying to sum first elements of the three vectors, second elements of the three vectors and finally third elements of the three vectors to get a new vector in the end containing this sums.
My code is the following:
(def matrix [[1 0 1] [0 1 0] [0 0 1]])
(def sum (atom 0))
(def sum-vector (atom []))
(for [i (range 3)]
((for [j (range (count matrix))]
(reset! sum (+ @sum (nth (nth matrix j) i)))
)
(reset! sum-vector (conj @sum-vector @sum))
(reset! sum 0)
))
But i get this exception : clojure.lang.LazySeq cannot be cast to clojure.lang.IFn ... What i'm i doing wrong?
Upvotes: 1
Views: 1281
Reputation: 18410
The problem is ((for ....))
That says evaluate the (for ....)
form, and then take that result and execute it. The (for ...)
is returning a LazySeq, instead of a function.
(apply map + [[1 0 1] [0 1 0] [0 0 1]])
;; (1 1 2)
If you want a vector returned instead of a LazySeq:
(apply mapv + [[1 0 1] [0 1 0] [0 0 1]])
;; [1 1 2]
Upvotes: 3