I think it's worthwhile to have a faster flatten even if it doesn't
look as elegant as the current implementation.  You could do a bit of
refactoring and save yourself a call to sequential? since the
recursive calls are guaranteed to have seqs (as a result of next).

Also, I'd prefer flatten to return the argument if it isn't
sequential? so for example, (flatten 10) ==> 10.  I think it would be
less likely to give mysterious results, especially with mistaken
arguments.  I understand that the current flatten for 1.2 beta doesn't
do this -- I'm just throwing in another suggestion after playing with
it for a while.

Here's my suggestion:

(defn fl1 "faster flatten" [coll]
  (letfn [(flcoll [coll]
                 (lazy-seq
                  (when-let [c (seq coll)]
                    (let [x (first c)
                          nxt (flcoll (next c))]
                      (if (sequential? x)
                        (concat (flcoll x) nxt)
                        (cons x nxt))))))]
    (if (sequential? coll) (flcoll coll) coll)))

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