I'm attempting to loop through a set of strings, stripping excess text. It works, but too well. Given input like this:
#{"TORCASO,133114,117,0.04,89045.38,99.00,0.00,0.00,0.00,0.00,0.00" "POTOCZNY,98770,170,0.06,87231.39,98.82,0.00,(S),0.00,0.00,(S)"}
My output looks like this:
#{\A \C \N \O \P \R \S \T \Y \Z}
But I expected it to look like this:
#{"TORCASO" "POTOCZNY"}
Here's my code:
(defn shorten-string
[string]
(first (str/split string #",")))
(defn handle-csv
[strings]
(loop [remaining-strings strings final-set #{}]
(if (empty? remaining-strings)
final-set
(let [[part & remaining] remaining-strings]
(recur remaining
(into final-set
(shorten-string part)))))))
What am I doing wrong?
CodePudding user response:
Using loop / recur for looping should be your last resort- there is usually some better function. In this case, you should use map:
(def data #{"TORCASO,133114,117,0.04,89045.38,99.00,0.00,0.00,0.00,0.00,0.00" "POTOCZNY,98770,170,0.06,87231.39,98.82,0.00,(S),0.00,0.00,(S)"})
(defn shorten-string [string]
(first (clojure.string/split string #",")))
(defn handle-csv [strings]
(->> strings
(map shorten-string)
(into #{})))
(->> is thread-last macro)
Example:
(handle-csv data)
=> #{"TORCASO" "POTOCZNY"}
CodePudding user response:
Apparently using "into" was the problem, as I was attempting to put a string into a set, so I should have been using conj or cons.
