On Feb 15, 2009, at 5:01 PM, Mark Engelberg wrote:

>
> On Sun, Feb 15, 2009 at 1:44 PM, Chouser <chou...@gmail.com> wrote:
>> (defn my-interpose [x coll]
>> (loop [v [x] coll coll]
>>   (if (seq coll)         ; Don't assume coll is a seq-or-nil
>>     (recur (-> v (conj (first coll)) (conj x)) (rest coll))
>>     v)))
>
>
> You know, there is an empty? predicate.  Why not write it as:
> (defn my-interpose [x coll]
>  (loop [v [x] coll coll]
>    (if (empty? coll) v         ; Don't assume coll is a seq-or-nil
>      (recur (-> v (conj (first coll)) (conj x)) (rest coll)))))
>
> I know that your first version is viewed as more idiomatic in Clojure,
> but I've never understood why Rich and others prefer that style.  It
> assumes that converting something to a seq is guaranteed to be a
> computationally cheap operation, and I see no reason to assume that
> will always be the case.   I can certainly imagine seq-able
> collections that take some time seq-ify, so converting to a seq to
> test for empty, and then just throwing it away causing it to be
> recomputed in rest doesn't seem as future-proof as just using empty?.
>

When walking a chain of seqs, empty? made no sense as there is no such  
thing as an empty seq. Now that rest returns a collection, this makes  
more sense (although still not my preference), but to each his own.  
Let's please not get bogged down in a style discussion now. You should  
be quite happy for this:

(rest [1])
-> () ;an empty sequence, note - not a canonic/sentinel value!

Also note empty? is still defined like this:

(not (seq coll))

Rich


--~--~---------~--~----~------------~-------~--~----~
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
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