Brian Hurt <bhur...@gmail.com> writes:

> I will note that vec and set both behave differently- when handed an empty
> sequence they don't return nil, they return the (not nil) empty vector or set
> (respectively).  The specific behavior of seq being depended upon in this case
> is different between seq and other, similar-purpose functions.

As Kevin mentioned, part of it historical, there used to be no empty
seqs:

http://clojure.org/lazy

Note also that vec and set are quite different functions to seq, they
take a sequence and create a *new* collection of a specific type
containing the elements of that sequence: 

(let [v [1 2 3]]
  (identical? v (vec v)))
;;=> false

(let [s '(1 2 3)]
  (identical? s (seq s)))
;;=> true

(let [v [1 2 3]]
  (identical? v (seq v)))
;;=> false

Seq on the other hand yields an ISeq "view" of a Seqable collection or
other seqable object (eg a String).  For objects that *are* already
non-empty seqs like lists, lazy-seqs and ranges it just returns the
object.  Note that a seq is not necessarily a persistent list.  The list
equivalent to the vec or set functions is (apply list ...) -- there's no
shorthand for it as you shouldn't be using lists much explicitly, use a
vector instead.

This might seem like a subtle distinction, but it's quite important.
For example map and reduce are seq functions, while get and conj are
collection functions.  Usually collection functions take the collection
as their first argument while seq functions take the seq as their last
argument.

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