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?.

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