0

The text files have a list of paths with a different prefix.

Lets say before.txt looks like this:

before/pictures/img1.jpeg
before/pictures/img2.jpeg
before/pictures/img3.jpeg

and after.txt looks like this:

after/pictures/img1.jpeg
after/pictures/img3.jpeg

The function deleted-files should remove the different prefix (before, after) and compare the two files to print the missing list of after.txt.

Code so far:

(ns dirdiff.core
(:gen-class))

(defn deleted-files [prefix-file1 prefix-file2 file1 file2]
    (let [before (slurp "resources/davor.txt")
    (let [after (slurp "resources/danach.txt")
)

Expected output: which is the one who was deleted

/pictures/img2.jpeg

How can I filter the lists in clojure.clj to show only the missing ones?

2
  • 2
    Please add the code you have tried and how it failed (e.g. errors, stacktraces, logs, ...) so we can improve on it. If you are looking for ideas, how to start, have a look at clojure.data/diff or clojure.set.
    – cfrick
    Feb 15, 2021 at 16:46
  • 2
    Please edit your question and tell us the results you expect for the given data. Feb 15, 2021 at 16:57

2 Answers 2

2

You probably want to compute a set difference between the two sets of filenames after prefices have been removed:

(defn deprefixing [prefix]
  (comp (filter #(clojure.string/starts-with? % prefix))
        (map #(subs % (count prefix)))))

(defn load-string-set [xf filename]
  (->> filename
       slurp
       clojure.string/split-lines
       (into #{} xf)))

(defn deleted-files [prefix-file1 prefix-file2 file1 file2]
  (clojure.set/difference (load-string-set (deprefixing prefix-file1) file1)
                          (load-string-set (deprefixing prefix-file2) file2)))

(deleted-files "before" "after"
               "/tmp/before.txt" "/tmp/after.txt")
;; => #{"/pictures/img2.jpeg"}
1

Here is how I would approach it, starting from this template project:

(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [clojure.set :as set]
    [tupelo.string :as str]
    ))

(defn file-dump->names
  [file-dump-str prefix ]
  (it-> file-dump-str
    (str/whitespace-collapse it)
    (str/split it #" ")
    (mapv #(str/replace % prefix "") it)))

(defn delta-files
  [before-files-in after-files-in
   before-prefix after-prefix]
  (let-spy [before-files     (file-dump->names before-files-in before-prefix)
            after-files      (file-dump->names after-files-in after-prefix)
            before-files-set (set before-files)
            after-files-set  (set after-files)
            delta-sorted     (vec (sort (set/difference before-files-set after-files-set)))]
    delta-sorted))

and a unit test to show it in action:

(dotest
  (let [before-files  "before/pictures/img1.jpeg
                       before/pictures/img2.jpeg
                       before/pictures/img3.jpeg "

        after-files   "after/pictures/img1.jpeg
                       after/pictures/img3.jpeg "
        before-prefix "before"
        after-prefix  "after"]
    (is= (delta-files before-files after-files before-prefix after-prefix)
      ["/pictures/img2.jpeg"])
    ))

Be sure to study the these documentation sources, including books like Getting Clojure and the Clojure CheatSheet.


Notes:

I like to use let-spy and let-spy-pretty to illustrate the progression of code. It produces output like so:

-------------------------------
   Clojure 1.10.2    Java 15
-------------------------------

Testing tst.demo.core
before-files => ["/pictures/img1.jpeg" "/pictures/img2.jpeg" "/pictures/img3.jpeg"]
after-files => ["/pictures/img1.jpeg" "/pictures/img3.jpeg"]
before-files-set => #{"/pictures/img3.jpeg" "/pictures/img2.jpeg" "/pictures/img1.jpeg"}
after-files-set => #{"/pictures/img3.jpeg" "/pictures/img1.jpeg"}
delta-sorted => ["/pictures/img2.jpeg"]

Ran 2 tests containing 1 assertions.
0 failures, 0 errors.

The spyx macro is also very useful for debugging. See the README and the API docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.