Search code examples
filehashclojurecomparelines

Clojure comparing lines in file


I have to write a clojure function, that compares lines in a file. My file contains information like this:

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

You can see it is written to define a hash. I want to import the hash in my program, but before doing so, I need to remove every line that is equal to other line. My lines need to be unique. How can I do this? I tried few things, but they were complete fails.


Solution

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

    Test:

    data.dat contains:

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

    result:

    (read-map-wo-dups "data.dat")
    => {:something1 1, :something2 2, :something3 3, :something4 4}