Rashi Bhalla
Rashi Bhalla

Reputation: 25

How to access elements of atom in clojure

(def input (atom[
#{
  [
    {:site-id 1} 
    {:both-unconf [[27 4] "Humidity"]} 
    {:own-unconf|other-conf [[30 1] "Humidity"]} 
    {:other-site-unconf 2500} 
    {:other-site-conf 2500} 
      [
        {:site-id 0} 
        {:both-unconf [[20 1] "Temperature"]} 
        {:own-unconf|other-conf [[22 0] "Temperature"]} 
        {:other-site-unconf 3} 
        {:other-site-conf 1}
      ]
  ]
} :something-else
]))

I want to access site-id from this atom map.

I am using the following code but not getting the output.

(->> @input
     (map #(:site-id %))
     (into [])

Upvotes: 0

Views: 231

Answers (1)

Svante
Svante

Reputation: 51501

(def input
  (atom [#{[{:site-id 1}
            {:both-unconf [[27 4] "Humidity"]} 
            {:own-unconf|other-conf [[30 1] "Humidity"]} 
            {:other-site-unconf 2500} 
            {:other-site-conf 2500} 
            [{:site-id 0} 
             {:both-unconf [[20 1] "Temperature"]} 
             {:own-unconf|other-conf [[22 0] "Temperature"]} 
             {:other-site-unconf 3} 
             {:other-site-conf 1}]]}
         :something-else]))

This data structure is very strange. It is

  • an atom, containing
  • a vector, containing
  • a set, containing
  • a vector, containing
  • a bunch of single-key maps, and then
  • another vector, containing
  • a bunch of single-key maps
  • etc., presumably

If we allow for the :something-else to be something else, it should be, to be useful:

  • an atom, containing
  • a vector, containing
  • a set, containing
  • a bunch of maps

So:

(def input
  (atom [#{{:site-id 1
            :both-unconf [[27 4] "Humidity"]
            :own-unconf|other-conf [[30 1] "Humidity"]
            :other-site-unconf 2500
            :other-site-conf 2500} 
           {:site-id 0
            :both-unconf [[20 1] "Temperature"]
            :own-unconf|other-conf [[22 0] "Temperature"]
            :other-site-unconf 3
            :other-site-conf 1}}
         :something-else]))

If it was like this, then

(->> @input
     first
     (mapv :site-id))

would work.

Upvotes: 1

Related Questions