Share your thoughts in the 2024 State of Clojure Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in Collections by

How to read a list of files, compare the text in the files with a set of keys, and return the key with the value true or false if that key is found in the file?

For example I have list of my files:

["src/cljs/project/cofx.cljs"
 "src/cljs/project/datetime.cljs"
 "src/cljs/project/core.cljs"
 "src/cljs/project/app_db.cljs"]

and set of keys:

#{:tooltip/terminate
  :member-services/description
  :event-errors/link-duplicated
  :firm-admin/pre-approved-applicants}

I want read all files (I used slurp), compare text from all files with first key and return this key and true and name of file if key exist in this file, or false if key not exist in all files.

for example:

[:tooltip/termiante true "src/cljs/project/cofx.cljs"
 :member-services/description false]

Do you have any tips? Because this task very difficult for me.

1 Answer

+1 vote
by
selected by
 
Best answer

Tips.

  • work from one file one key, then extend solution to work with multiple files and then with multiple keys
  • slurp return you full file contents as a string. This mean that your task is effectively reduce to the running
    (clojure.string/includes? (slurp file) key-as-string)
  • building on top of this you can create function that will create you a small vectors

    (fn [key file] 
    (if (clojure.string/includes? (slurp file) (str key))
        [key true file]
        [key false])
    
  • to repeat any action based on input collection you will need to map this function over your file collection.

...