0

I have an atom like this, inside a let:

(let [scors (atom {:one   {:year-one [] :year-five [] :year-ten []}
                   :two   {:year-one [] :year-five [] :year-ten []}
                   :three {:year-one [] :year-five [] :year-ten []})]
    
some code...
)

Inside a for loop, I want to swap this atom to add data depends on the if statements inside that loop.

For example I tried this, but it didn't work:

(swap! scors :assoc {:one :year-one} (:set-of-inputs each)))

So for example I want to add {:name "someone"} to :one :year-one. How can I update the :year-one?

Thank you in advance.

niloofar
  • 2,244
  • 5
  • 23
  • 44
  • *"Inside a for loop, I want to swap this atom"*: usually, it is a good idea to avoid side effects inside lazy constructs such as `for`-loops. If you make it a regular `loop`, the map could be a loop variable and you wouldn't need to wrap it inside an atom. Without seeing the code, it is hard to tell what is the best approach. – Rulle Apr 05 '23 at 05:53
  • This smells of X-Y-problem: Clojure has no "for-loop" (it has a lazy list comprehension named `for`). Using an `atom` in a `let` is rarely needed or a good approach. If you try to solve a problem the "imperative way", it would really help to know the problem you are solving and you will very likely get a better answer with idiomatic Clojure code. – cfrick Apr 05 '23 at 07:49
  • @Rulle as I need something like for each, to generate a combintaion of some vectors, I'm using for to do that. – niloofar Apr 07 '23 at 18:33

1 Answers1

2

Use swap! with update-in

(swap! scors update-in [:one :year-one] conj (:set-of-inputs each))
erdos
  • 3,135
  • 2
  • 16
  • 27