Skip to content

Instantly share code, notes, and snippets.

@conf8o
Last active December 1, 2020 08:38
Show Gist options
  • Save conf8o/fce7d9a9f6f24757408fd860ae50beb1 to your computer and use it in GitHub Desktop.
Save conf8o/fce7d9a9f6f24757408fd860ae50beb1 to your computer and use it in GitHub Desktop.
A macro evaluates body, like with-open. Names are bound to line-seqs of readers.
(defmacro with-read-lines [reader bindings & body]
(cond
(= (count bindings) 0) `(do ~@body)
(symbol? (bindings 0)) `(let* [rdr# (~reader ~(bindings 1))
~(bindings 0) (line-seq rdr#)]
(try
(with-read-lines ~reader ~(subvec bindings 2) ~@body)
(finally
(. rdr# close))))
:else (throw (IllegalArgumentException.
"with-read-lines only allows Symbols in bindings"))))
(defn eg []
(with-read-lines clojure.java.io/reader [x "x.txt"
y "y.txt"]
(println (vec x))
(println (vec y)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment