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. Theclojure.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:
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,
leverage the
AsynchronousFileChannel
of Java pluscore.async
to maybe parallelize the work? Not sure how this would work.
Inspiration
gist
Top comments (1)
Here’s a possible answer that refines your initial program using Clojure's core.async and Java's AsynchronousFileChannel to speed up the search for words containing the letter "a" across multiple documents. The idea is to stop scanning a document as soon as an "a" is found, and to parallelize the processing of documents.