On Dec 2, 12:06 pm, ataggart <alex.tagg...@gmail.com> wrote:
> On Dec 2, 7:10 am, Stefan Kamphausen <ska2...@googlemail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > while studying the collection types and trying to find out which
> > functions work on all collection types (i.e. lists, vectors, maps,
> > sets) I was flabbergasted by the following
>
> > user> (contains? (list 1 2 3) 3)
> > false
>
> > OK, the doc of contains? told me that for indexed collection-types it
> > will only check, whether the index is within the valid range.  So
> > maybe:
>
> > user> (contains? (list 1 2 3) 1)
> > false
>
> > At that point I dived into the implementation and found that in
> > RT.java checks for the type of collection passed but leaves out
> > PersistenList.
>
> > I'd like to understand the (probably well-grounded) reason for that.
> > As far as I can see PersistentList extends Counted, so the check for
> > the index-range should at least be possible.  However, I think people
> > would expect an equality check in the background, which OTOH would be O
> > (N), probably.
>
> > Kind regards,
> > Stefan
>
> That's because contains? checks for keys not values.  Since a list
> isn't associative (unlike vectors and maps), it doesn't have keys,
> thus contains? doesn't apply.  I too was first thrown off by assuming
> contains? worked on the values of a collection; for that you can
> instead use some.
>
> (if (some #(= :a) '(:a :b :c))
>   :contains-a
>   :does-not-contain-a

Erm...  that predicate should be #(= :a %) or (partial = :a) or #{:a}.

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