On Wed, Jul 27, 2011 at 10:51 AM, Dmitry Gutov <[email protected]> wrote:
> This would be a straightforward solution:
>
> (defmacro when-lets [bindings & body]
> `(let ~bindings
> (when (and ~@(map first (partition 2 2 bindings)))
> ~@body)))
>
> It works well in simple cases, but breaks e.g. in case of parameter
> destructuring.
> If you read `(source when-let)`, you'll see that it uses a temporary
> binding. So we can add that and a loop, or just reuse `when-let` and
> use something akin to recursion:
>
> (defmacro when-lets [bindings & body]
> (if (empty? bindings)
> `(do ~@body)
> `(when-let [~@(take 2 bindings)]
> (when-lets [~@(drop 2 bindings)]
> ~@body))))
>
> user=> (when-lets [a 1 [b c] [1 2]] (+ a b c))
> 4
> user=> (when-lets [a 1 [b c] nil] (+ a b c))
> nil
>
> Questions?
I'd go with the latter approach. The former has the problem that we'd
really like this:
(when-lets [wr (:key some-map-of-weak-references)
v (.get wr)]
(do-something-with v))
not to blow up when :key isn't found in the map.
--
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en