DEV Community

Kevin Mungai
Kevin Mungai

Posted on

Programming Problem

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])
Enter fullscreen mode Exit fullscreen mode

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 (0)