Hi Mark,

Am 21.12.2008 um 16:51 schrieb Mark Volkmann:

Until this morning I was under the impression that ALL Clojure
collections are sequences. Now I understand (from the screencast
"Clojure Data Structures - Part 2") that vectors and maps are not and
that you need to call "seq" on them to get a sequence representation.
What are some situations where this is necessary? I'm confused because
the following code works.

It is important to understand the difference between a collection
and a seq. Collections are specific implementations of a container.
Maybe a vector, maybe a map, ...

A seq on the other hand is an abstract view on such a collection.
(In particular a seq is not a collection.)

Note that  a list is still a collection, which just happens to implement
the contract of a seq. So it can act as it's own seq.

(def my-map {:a 1 :b 2 :c 3})
(first my-map) -> [:b 2]
(rest my-map} -> ([:c 3] [:a 1])


Theoretically the calls would need a seq for the map.

(first (seq my-map))

However, this is very hard on the user. He always has to take
care, that he passes a seq. But since a seq is also it's own seq,
ie. (seq a-seq) == a-seq, we can change the interface from
"takes a seq" to "takes a sequable thing" and call seq on that
thing in the function itself.

Then we suddenly save a lot of seq calls on the user side.
Also being more convenient for the user.

Hope this helps.

Sincerely
Meikel

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to