Clojure比较文件中的行

时间:2013-01-12 17:46:05

标签: file hash clojure compare lines

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

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

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

2 个答案:

答案 0 :(得分: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}

答案 1 :(得分: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"}