HB <hubaghd...@gmail.com> writes:

> Ken & Alex,
> Why you aren't calling empty? when you want to check if a collection
> is empty?

Here's the definition of empty? from clojure/core.clj:

    (defn empty?
      "Returns true if coll has no items - same as (not (seq coll)).
      Please use the idiom (seq x) rather than (not (empty? x))"
      [coll] (not (seq coll)))
    
So this:

    (if (not (empty? coll)) ...)

is really:

    (if (not (not (seq coll))) ...)

;-)

The other reason I do it that way, is that for a non-sequence collection
(like a hash table or Java array), seq will convert it into a sequence.
The premature optimizer in me likes to avoid that conversion happening
twice -- otherwise both the "empty?" and "rest" calls will call seq to
convert the collection into a sequence.

Hence this form:

    (if-let [s (seq coll)] ...)

Which is just a shorthand for:

    (let [s (seq coll)]
      (if s ...))

In reality it probably makes no practical difference of course.  I'm
just in the habit of it.

The best way is to use "reduce" and let it take care of all the details
for you. ;-)

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