I need to read file to map {:v '[] :f '[]}. I split each line and if first element "v" then I add remaining part to v-array, same for f-array.
Example:
v 1.234 3.234 4.2345234
v 2.234 4.235235 6.2345
f 1 1 1
Expected result:
{:v [("1.234" "3.234" "4.2345234"), ("2.234" "4.235235" "6.2345")]
:f [("1" "1" "1")]}
My result:
{:v [("2.234" "4.235235" "6.2345")]
:f [("1" "1" "1")]}
Questions:
- How can I fix error? (only last line was added to map)
- Can I avoid global variable (model) and side effects?
Code:
(def model
{:v '[]
:f '[]})
(defn- file-lines
[filename]
(line-seq (io/reader filename)))
(defn- lines-with-data
[filename]
(->>
(file-lines filename)
(filter not-empty)
(filter #(not (str/starts-with? % "#")))))
(defn- to-item [data]
(let [[type & remaining] data]
(case type
"v" [:v (conj (:v model) remaining)]
"f" [:f (conj (:f model) remaining)])))
(defn- fill-model
[lines]
(into model
(for [data lines] (to-item data))))
(defn parse
[filename]
(->>
(lines-with-data filename)
(map #(str/split % #"\s+"))
(fill-model)))