0
votes

I'm new in Clojure. I have this problems:

I receive this data from a function:

({:lat 40.4167754, :lng -3.7037902, :address Madrid, Spain})  

When I ask for the class, I get:

> class x
> clojure.lang.LazySeq

I need access to :lat, :lng, :address, but I don't know how.

1
Should the sequence always contain a single value? You can use (first x) to get the first map. - Lee
Your data is not valid. I'd expect ({:lat 40.4167754, :lng -3.7037902, :address "Madrid, Spain"}), with a preliminary ' if you expect to read it. - Thumbnail
Thanks @Lee that is the key to solve my problem. - matiasmasca

1 Answers

1
votes

Try this:

(defn mystery-fn []
  (list {:lat 40.4167754, :lng -3.7037902, :address "Madrid, Spain"} )
)

(println :println (mystery-fn))
(prn     :prn     (mystery-fn))

(def a (first (mystery-fn)))
(prn :a a)

(def b (:lat a))
(prn :b b)

with output:

:reloading (tst.clj.core)
:println ({:lat 40.4167754, :lng -3.7037902, :address Madrid, Spain})
:prn ({:lat 40.4167754, :lng -3.7037902, :address "Madrid, Spain"})
:a {:lat 40.4167754, :lng -3.7037902, :address "Madrid, Spain"}
:b 40.4167754

Notice the difference between println and prn. Using prn, you get strings displayed with double-quotes which can help a lot when there are embedded spaces.

Also, when you want to label a printed output, it is often easier to use a keyword as the label like (prn :xyz ...) instead of (println "xyz = " ...).