or9ob
or9ob

Reputation: 2392

Clojure: use vector within an atom?

I have created an atom, that contains a vector:

(def name-seq (atom ["A" 1]))

Within the swap! operation, I need to increment the number that's the last part of the vector. Here's what I am trying:

(swap! name-seq #(["A" (inc (last @%))]))

I get the following error: ClassCastException clojure.lang.PersistentVector cannot be cast to java.util.concurrent.Future clojure.core/deref-future (core.clj:2108)

What am I doing wrong here?

Upvotes: 0

Views: 1270

Answers (2)

Leonid Beschastny
Leonid Beschastny

Reputation: 51450

If your name-seq is a fixed-length vector, then you may use update-in function to do so:

(swap! name-seq #(update-in % [1] inc))

Upvotes: 1

or9ob
or9ob

Reputation: 2392

Thanks to @loki for the answer via a comment. The swap! function sends the deref-ed atom to the swapping function. Hence I needed to remove the deref that I was doing with @ from my solution:

(swap! name-seq #(["A" (inc (last %))])).

Upvotes: 1

Related Questions