Hi all,

On Sat, Oct 17, 2009 at 2:12 AM, John Harrop <jharrop...@gmail.com> wrote:

> On Fri, Oct 16, 2009 at 7:58 PM, mbrodersen <morten.broder...@gmail.com>wrote:
>
>>
>> Hi,
>>
>> I am new to Clojure and I am wondering if there is anything similar to
>> the following macro already built in:
>>
>> (defmacro let-while
>>        "Makes it easy to continue processing an expression as long as it
>> is
>> true"
>>        [[name expr] & forms]
>>        `(loop []
>>                (let [~name ~expr]
>>                        (when ~name
>>                                ~...@forms
>>                                (recur)))))
>>
>
> Nice.
>
> But name is multiply evaluated.
>


It's a valid point: since name is used in test-position in the chen clause
we must ensure that's it's a symbol and not a destructuring form (map or
vector).
eg
  (let-while [[a b] nil] (println "you should never see this message"))

But the proposed version doesn't solve anything.

Here is a better one:
(defmacro while-let
 "Makes it easy to continue processing an expression as long as it is true"
 [binding & forms]
  `(loop []
     (when-let ~binding
       ~...@forms
       (recur))))

(I cheated: rather than manually working around this bug, I used when-let
which already does the right thing.)

hth,

Christophe


-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to