Because seq is defined as returning nil for an empty sequence. The
only way to find that out for a lazy sequence is to realize the first
element.
I'm not sure if that answers why seq should realize the first
element. Even by what you say, only if I wanted to find if my LazySeq
was nil should I realize its first element.
My understanding of seq is - restricting some Collection type
abstraction to the Sequence abstraction (but maybe my thinking is
wrong). When I say (seq coll) I don't mean - 1) convert it into a
sequence abstraction + 2) go and find out if the coll is empty by
realizing its first element.
You should probably read this:
http://clojure.org/lazy
Your original mail said:
I have some conceptual questions on the sequence abstraction. I
understand that (seq coll) will give me a "sequence".
coll maybe be a list, vector, map, set, LazySeq or nil.
Your understanding of seq does not match up to (current) reality. In
particular:
"""
Changed: the seq function - no longer an identity for ISeqs
• still returns either a seq or nil
• (seq aseq) -> no longer an identity, if aseq empty returns nil
• still works on nil
"""
That doc might help explain.
The point of all this is that calling seq on something will always
return either nil or a non-empty sequence. That's quite useful,
because a sequence is 'true' and nil is 'false'.
lazy-seq might do what you're trying to do: take anything and give you
a sequence.
user=> (seq? (lazy-seq nil))
true
user=> (seq? nil)
false
On the other hand, you probably ought to consider whether you need to
do this at all -- Clojure is abstraction-oriented, so your code should
be happy to receive anything sequential, not just Seqs.
Look at the sequential? predicate.
user=> (sequential? (lazy-seq [1 2 3]))
true
user=> (sequential? [1 2 3])
true
user=> (sequential? [])
true
user=> (sequential? (lazy-seq []))
true
--
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