0
votes

I am very new to closure so I am not fully sure how to do this. If I have a file data.txt with the following:

[
{:name "Steve"}
{:name "Issac"}
{:name "Lucas"}
{...}
]

I want to be able to read the contents of each :name tag and do something with the return value (in this case it will be printing to the console). I looked up online and found there is a method called reader and I understand how to open a file.

The Closure syntax confuses me slightly so I am not sure how to do this.

1

1 Answers

0
votes

there should be 2 possiblities:

1) raw clojure by means clojure.core/read-string

(read-string  "['q 2 \"test\"]")
;; [(quote q) 2 "test"]

2) via clojure.edn/read-string

(clojure.edn/read-string "['q 2 \"test\"]")
;; ['q 2 "test"]

the 2nd one should be faster and safer (but does not eval and stuff), but is only for edn format (this is a subset of clojure code)


the string dummy (i.e from your data.txt)

;; a string, just for demo
(def s "[{:name \"Steve\" email: \"[email protected]\" }
         {:name \"Issac\"}
         {:name \"Lucas\"}]")

the rest is plain clojure, if you have trouble with clojure maps here is the doc

(doseq [name (map :name (clojure.edn/read-string s))]
  (println name))
;; Steve
;; Issac
;; Lucas
;; nil