Search code examples
clojurelazy-evaluationseq

Clojure read the content of a lazy-seq ({ })


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.


Solution

  • 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 = " ...).