0

我必须编写一个 clojure 函数来比较文件中的行。我的文件包含如下信息:

{:something1 1
    :something2 2
    :something2 2
    :something3 3
    :something4 4
    :something4 4
}

您可以看到它是为定义哈希而编写的。我想在我的程序中导入哈希,但在这样做之前,我需要删除与其他行相等的每一行。我的线条必须是独一无二的。我怎样才能做到这一点?我尝试了几件事,但它们完全失败了。

4

2 回答 2

1
(defn read-map-wo-dups [fname]
  (into {}
   (with-open [r (reader fname)]
     (doall (distinct
             (map #(read-string
                    (str "[" (replace % #"[{}]" "") "]"))
                  (line-seq r)))))))

测试:

data.dat包含:

{:something1 1
 :something2 2
 :something2 2
 :something3 3
 :something3 3
 :something4 4}

结果:

(read-map-wo-dups "data.dat")
=> {:something1 1, :something2 2, :something3 3, :something4 4}
于 2013-01-12T18:22:49.817 回答
1

这可以分解成更简单的步骤,然后穿入一个简单的“单线”

(->> (slurp "data")         ; read the data from the file.
     (re-seq #"[^{} \n]+")  ; split it into strings ignoring \n and { }.
     (partition 2)          ; group it into key, value pairs
     (map vec)              ; turn the pairs into vectors because into wants this.
     (into {}))             ; mash them in turn into a single map.

{":something1" "1", ":something2" "2", ":something3" "3", ":something4" "4"}

或者,如果您更喜欢嵌套形式,您可以编写相同的代码,如下所示:

user> (into {} (map vec (partition 2 (re-seq #"[^{} \n]+" (slurp "data")))))
{":something1" "1", ":something2" "2", ":something3" "3", ":something4" "4"}
于 2013-01-12T19:38:27.250 回答