summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2008-12-23 14:02:00 +0000
committerRich Hickey <richhickey@gmail.com>2008-12-23 14:02:00 +0000
commit7fdd2d0b3aa66d2b2cd13530bc027d5bbdbe923c (patch)
tree91d0b7801b088a23f5b0d00c68615e0a08e0ce43
parentef945679e95e08e03d4c8b3cd62b6c09ef08e996 (diff)
with-open accepts multiple bindings, patch from Meikel Brandmeyer
-rw-r--r--src/clj/clojure/core.clj43
1 files changed, 24 insertions, 19 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 0b58b4ff..be452aa4 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -1817,18 +1817,34 @@
"Reads one object from the string s"
[s] (clojure.lang.RT/readString s))
+(defn subvec
+ "Returns a persistent vector of the items in vector from
+ start (inclusive) to end (exclusive). If end is not supplied,
+ defaults to (count vector). This operation is O(1) and very fast, as
+ the resulting vector shares structure with the original and no
+ trimming is done."
+ ([v start]
+ (subvec v start (count v)))
+ ([v start end]
+ (. clojure.lang.RT (subvec v start end))))
+
(defmacro with-open
- "bindings => name init
+ "bindings => [name init ...]
- Evaluates body in a try expression with name bound to the value of
- init, and a finally clause that calls (.close name)."
+ Evaluates body in a try expression with names bound to the values
+ of the inits, and a finally clause that calls (.close name) on each
+ name in reverse order."
[bindings & body]
(if (vector? bindings)
- `(let ~bindings
- (try
- ~@body
- (finally
- (.close ~(first bindings)))))
+ (cond
+ (= (count bindings) 0) `(do ~@body)
+ (symbol? (bindings 0)) `(let ~(subvec bindings 0 2)
+ (try
+ (with-open ~(subvec bindings 2) ~@body)
+ (finally
+ (. ~(bindings 0) close))))
+ :else (throw (IllegalArgumentException.
+ "with-open only allows Symbols in bindings")))
(throw (IllegalArgumentException.
"with-open now requires a vector for its binding"))))
@@ -2023,17 +2039,6 @@
[s key]
(. clojure.lang.PersistentStructMap (getAccessor s key)))
-(defn subvec
- "Returns a persistent vector of the items in vector from
- start (inclusive) to end (exclusive). If end is not supplied,
- defaults to (count vector). This operation is O(1) and very fast, as
- the resulting vector shares structure with the original and no
- trimming is done."
- ([v start]
- (subvec v start (count v)))
- ([v start end]
- (. clojure.lang.RT (subvec v start end))))
-
(defn load-reader
"Sequentially read and evaluate the set of forms contained in the
stream/file"