为什么我的 clojure 循环返回单个字母而不是整个单词?

Why is my clojure loop returning single letters instead of whole words?

我正在尝试遍历一组字符串,去除多余的文本。它有效,但太好了。给出这样的输入:

#{"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)"}

我的输出如下所示:

#{\A \C \N \O \P \R \S \T \Y \Z}

但我希望它看起来像这样:

#{"TORCASO" "POTOCZNY"}

这是我的代码:

(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)))))))

我做错了什么?

显然使用“into”是个问题,因为我试图将一个字符串放入一个集合中,所以我应该使用 conj 或 cons。

使用 loop / recur 进行循环应该是您最后的选择 - 通常有一些更好的功能。在这种情况下,您应该使用 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 #{})))

->> 是 thread-last 宏)

示例:

(handle-csv data)
=> #{"TORCASO" "POTOCZNY"}