Permalink
Browse files

avoid double evaluation in when-first

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent 2f0c86e commit d05cc87f2787c93166c025284470f0ffd50dc3dd @miner miner committed with stuarthalloway Sep 3, 2012
Showing with 4 additions and 4 deletions.
  1. +4 −4 src/clj/clojure/core.clj
View
@@ -4128,16 +4128,16 @@
(defmacro when-first
"bindings => x xs
- Same as (when (seq xs) (let [x (first xs)] body))"
+ Roughly the same as (when (seq xs) (let [x (first xs)] body)) but xs is evaluated only once"
{:added "1.0"}
[bindings & body]
(assert-args
(vector? bindings) "a vector for its binding"
(= 2 (count bindings)) "exactly 2 forms in binding vector")
(let [[x xs] bindings]
- `(when (seq ~xs)
- (let [~x (first ~xs)]
- ~@body))))
+ `(when-let [xs# (seq ~xs)]
+ (let [~x (first xs#)]
+ ~@body))))
(defmacro lazy-cat
"Expands to code which yields a lazy sequence of the concatenation

0 comments on commit d05cc87

Please sign in to comment.