Last active
December 1, 2020 08:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a | |
b | |
c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
2 | |
3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment