DEV Community

loading...

Programming Problem

kevinmungai profile image Kevin Mungai ・1 min read

Write a program that given a number of documents, can find all the documents with words containing the letter "a" in them.

Using the with-open macro that will automatically close any I/O. The clojure.string/includes? function will check each line as it comes in if it contains the letter "a" and will return true or false.

(defn contains-a?
  [document]
  (with-open [rdr (clojure.java.io/reader document)]
    (clojure.string/includes? (line-seq rdr) "a")))

(filter #(contains-a? %) [documents])

Next step is to:

  1. try and succeed as fast as possible, i.e once the program detects an "a" it should return true and stop looking through that file and,

  2. leverage the AsynchronousFileChannel of Java plus core.async to maybe parallelize the work? Not sure how this would work.

Inspiration
gist

Discussion

pic
Editor guide