Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my stab at a functional-ish approach to the problem:</p> <ul> <li>Create 2 maps, one for each file</li> <li>Find the missing items between the two maps, using <code>dissoc</code></li> <li>Find the different, but shared items between the two maps, using <code>intersection</code> and <code>filter</code></li> </ul> <h2>Code</h2> <pre><code>(ns diffset (:use [clojure.contrib.duck-streams] [clojure.set])) (def file1 "bookmarks.csv") (def file2 "bookmarks2.csv") (defn split-record [line] "split line into (id, bookmark)" (map #(apply str %) (split-with #(not (= % \|)) line))) (defn map-from-file [f] "create initial map from file f" (with-open [r (reader f)] (doall (apply hash-map (apply concat (map split-record (line-seq r))))))) (defn missing [x y] "return seq of all ids in x that are not in y" (keys (apply dissoc x (keys y)))) (defn different [x y] "return seq of all ids that match but have different bookmark string" (let [match-keys (intersection (set (keys x)) (set (keys y)))] (filter #(not (= (get x %) (get y %))) match-keys))) (defn diff [file1 file2] "print out differences between two bookmark files" (let [[s1 s2] (map map-from-file [file1 file2])] (dorun (map #(println (format "Id #%s is missing in set #1" %)) (missing s2 s1))) (dorun (map #(println (format "Id #%s is missing in set #2" %)) (missing s1 s2))) (dorun (map #(println (format "Id #%s is different:" %) "\n" " -&gt;" (get s1 %) "\n" " -&gt;" (get s2 %)) (different s1 s2))))) </code></pre> <h2>Result</h2> <pre><code>user&gt; (use 'diffset) nil user&gt; (diff file1 file2) Id #1 is missing in set #1 Id #5 is missing in set #2 Id #3 is different: -&gt; |www.msnbc.com|Search| -&gt; |www.msnbc.com|Search|New Comment nil </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload