On Mon, Aug 25, 2008 at 9:46 AM, Rich Hickey <[EMAIL PROTECTED]> wrote:

>
> On Aug 22, 3:59 pm, "Shawn Hoover" <[EMAIL PROTECTED]> wrote:
> > While thinking about drewr's map destructuring question on IRC, I found
> that
> > nth doesn't work on maps. Other group emails state this fact but I can't
> > find that it's by design. (doc nth) says it works on sequences. Maps do
> work
> > with seq, first, rest, etc, so it seems like nth should accept maps.
> >
>
> There is a difference between a sequential view of a collection (i.e.
> what you get from seq) and the collection itself. Maps are not
> sequential data structures. nth is only supported for sequential data
> structures, and that is by design.
>
> If in some situation it makes sense to treat a map as sequential (it
> might make some sense with array maps or sorted maps), just use (seq
> m), which will serve as an indicator of that special point of view.
>

Intuitively I knew that maps weren't inherently sequential, but when I saw
that first and rest worked on them and saw how nth was implemented, I kept
thinking nth should join the "I'll call seq for you" free-for-all. I'll buy
it since it's by design.

Based on this discussion, the docs, and what I gathered from the
implementation, my current model is:

   1. Data structures such as vectors, lists, strings, and Arrays are
   inherently sequential. This property is denoted case-by-case in the docs and
   the implementation of nth and formally by the interface
   clojure.lang.Sequential. The latter happens to be implemented by
   IPersistentVector, IPersistentList, and ASeq.
   2. Sequences are logical sequential views of other data structures that
   are not inherently sequential, such as maps and hash-sets. This capability
   is obtained by implementing ISeq (first, rest, cons) and, by inheritance,
   IPersistentCollection (seq, count, a different cons).
   3. nth provides direct support for inherently sequential things and for
   sequences. However, nth is not part of the seq library; you must explicitly
   pass the seq of anything else. nth happens to cast instances of Sequential
   to IPersistentCollection in order to call seq and get the nth rest, but this
   is an implementation detail and does not mean that nth supports directly all
   instances of ISeq or IPersistentCollection.
   4. Vector binding destructuring works on things that nth supports.
   5. The bottom line is that nth accepts sequences but it will not call seq
   for you if you pass anything else.

Sorry if this is too focused on the implementation, but for some reason that
was key for me to get out of my head that nth should call seq on everything.

Thanks for explaining,
Shawn

--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to