Re: How to write `when-lets`

2011-08-05 Thread Shen, Feng
I learned a lot. Thanks. Best Regards Shen Feng On Wed, Jul 27, 2011 at 8:50 PM, Feng Shen wrote: > Clojure core.clj has a macro when-let, > I am wondering how to write a macro `when-lets` > > (when-lets [symbol-1 test-1 > symbol-2 test-2 >... >] >

Re: How to write `when-lets`

2011-07-27 Thread Oskar
On Jul 27, 9:23 pm, Alan Malloy wrote: > On Jul 27, 11:56 am, Dmitry Gutov wrote: > > > > First: Why doesn't macroexpand expand the inner when-lets? > > > It's not supposed to, see the doc. To do full expansion, you can use > > `clojure.walk/macroexpand-all`. > > > > Is the gensym in the two ex

Re: How to write `when-lets`

2011-07-27 Thread Oskar
On Jul 27, 8:56 pm, Dmitry Gutov wrote: > > First: Why doesn't macroexpand expand the inner when-lets? > > It's not supposed to, see the doc. To do full expansion, you can use > `clojure.walk/macroexpand-all`. Oh, yeah. Thanks! > > Is the gensym in the two expands the same thing, or do "they"

Re: How to write `when-lets`

2011-07-27 Thread Ken Wesson
On Wed, Jul 27, 2011 at 3:23 PM, Alan Malloy wrote: > On Jul 27, 11:56 am, Dmitry Gutov wrote: >> > First: Why doesn't macroexpand expand the inner when-lets? >> >> It's not supposed to, see the doc. To do full expansion, you can use >> `clojure.walk/macroexpand-all`. >> >> > Is the gensym in the

Re: How to write `when-lets`

2011-07-27 Thread Ken Wesson
On Wed, Jul 27, 2011 at 2:57 PM, Aaron Cohen wrote: > On Wed, Jul 27, 2011 at 2:39 PM, Ken Wesson wrote: >> >> On Wed, Jul 27, 2011 at 2:35 PM, Aaron Cohen wrote: >> > I may be wrong, but don't you need to swap the order of the arguments to >> > f? >> >> You can do that by writing f itself appro

Re: How to write `when-lets`

2011-07-27 Thread Alan Malloy
On Jul 27, 11:56 am, Dmitry Gutov wrote: > > First: Why doesn't macroexpand expand the inner when-lets? > > It's not supposed to, see the doc. To do full expansion, you can use > `clojure.walk/macroexpand-all`. > > > Is the gensym in the two expands the same thing, or do "they" get the > > same na

Re: How to write `when-lets`

2011-07-27 Thread Alan Malloy
On Jul 27, 11:57 am, Aaron Cohen wrote: > On Wed, Jul 27, 2011 at 2:39 PM, Ken Wesson wrote: > > On Wed, Jul 27, 2011 at 2:35 PM, Aaron Cohen wrote: > > > I may be wrong, but don't you need to swap the order of the arguments to > > f? > > > You can do that by writing f itself appropriately. Usua

Re: How to write `when-lets`

2011-07-27 Thread Aaron Cohen
On Wed, Jul 27, 2011 at 2:39 PM, Ken Wesson wrote: > On Wed, Jul 27, 2011 at 2:35 PM, Aaron Cohen wrote: > > I may be wrong, but don't you need to swap the order of the arguments to > f? > > You can do that by writing f itself appropriately. Usually either it > will be a closure or it won't matt

Re: How to write `when-lets`

2011-07-27 Thread Dmitry Gutov
> First: Why doesn't macroexpand expand the inner when-lets? It's not supposed to, see the doc. To do full expansion, you can use `clojure.walk/macroexpand-all`. > Is the gensym in the two expands the same thing, or do "they" get the > same name? That was surprising to me. I can't think of any re

Re: How to write `when-lets`

2011-07-27 Thread Ken Wesson
On Wed, Jul 27, 2011 at 2:35 PM, Aaron Cohen wrote: > I may be wrong, but don't you need to swap the order of the arguments to f? You can do that by writing f itself appropriately. Usually either it will be a closure or it won't matter (core +, etc.). -- Protege: What is this seething mass of p

Re: How to write `when-lets`

2011-07-27 Thread Aaron Cohen
On Wed, Jul 27, 2011 at 2:15 PM, Ken Wesson wrote: > On Wed, Jul 27, 2011 at 2:13 PM, Alan Malloy wrote: > > On Jul 27, 11:11 am, Alan Malloy wrote: > >> On Jul 27, 5:50 am, Feng Shen wrote: > >> > > (defn foldr > ([f coll] >(reduce f (reverse coll))) > ([f init coll] >(reduce f init

Re: How to write `when-lets`

2011-07-27 Thread Ken Wesson
On Wed, Jul 27, 2011 at 2:13 PM, Alan Malloy wrote: > On Jul 27, 11:11 am, Alan Malloy wrote: >> On Jul 27, 5:50 am, Feng Shen wrote: >> >> > Clojure core.clj has a macro when-let, >> > I am wondering how to write a macro `when-lets` >> >> > (when-lets [symbol-1 test-1 >> >                    sy

Re: How to write `when-lets`

2011-07-27 Thread Alan Malloy
On Jul 27, 11:11 am, Alan Malloy wrote: > On Jul 27, 5:50 am, Feng Shen wrote: > > > Clojure core.clj has a macro when-let, > > I am wondering how to write a macro `when-lets` > > > (when-lets [symbol-1 test-1 > >                    symbol-2 test-2 > >             ... > >             ] > >      

Re: How to write `when-lets`

2011-07-27 Thread Alan Malloy
On Jul 27, 5:50 am, Feng Shen wrote: > Clojure core.clj has a macro when-let, > I am wondering how to write a macro `when-lets` > > (when-lets [symbol-1 test-1 >                    symbol-2 test-2 >             ... >             ] >            body >            ) > > body only get evaluated when (

Re: How to write `when-lets`

2011-07-27 Thread Oskar
I tried to solve this, and, having very litte macro-writing experience, just looked at when-let and tried to modify it to work with multiple binds. This is what I ended up with: (defmacro when-lets [bindings & body] (if-not (seq bindings) `(do ~@body) (let [form (bindings 0) tst (bindi

Re: How to write `when-lets`

2011-07-27 Thread Ken Wesson
On Wed, Jul 27, 2011 at 10:51 AM, Dmitry Gutov 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 pa

Re: How to write `when-lets`

2011-07-27 Thread Dmitry Gutov
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

How to write `when-lets`

2011-07-27 Thread Feng Shen
Clojure core.clj has a macro when-let, I am wondering how to write a macro `when-lets` (when-lets [symbol-1 test-1 symbol-2 test-2 ... ] body ) body only get evaluated when (and test-1 test-2 ) I am thinking about it, anybody ha