stream-file-lines-with-closing
Clojure
(require '[clojure.java.io :as io]
'[clojure.string :as str])
(defn each-line
"Calls f with each line of a UTF-8 file, closing the reader afterwards."
[path f]
(with-open [rdr (io/reader (io/file path) :encoding "UTF-8")]
(doseq [line (line-seq rdr)]
(f line))))
(defn count-non-blank [path]
(let [n (atom 0)]
(each-line path (fn [line] (when-not (str/blank? line) (swap! n inc))))
@n))