Clojure
user=> (.contains [1 2 3] 2)
true
user=> (true? (.contains [1 2 3] 2))
false
AFAICS true? is implemented using identical? which tests by reference
equality. Now since Java boolean values are boxed into Booleans we have
not only Boolean.TRUE. Maybe true? (and false?) should be implemented in
terms of equals?
But I agree it seems odd.
No. contains? is meant for maps, i.e. when applied to vectors it checks
against indexes:
user=> (contains? [1 2 3] 0)
true
I propose that contains? checks values on vectors, leaving the
behavior as-is for other collection types.
And yes, I understand the existing behavior is consistent (at the data
structure level). But it doesn't meet user expectations.
[..]
> Possible models are CL's member, which works on lists and returns the
> tail beginning with the found item, and position, which works on
> sequences and returns the index if found. There are also versions of
> each that use predicate functions.
>
> http://www.lispworks.com/documentation/HyperSpec/Body/f_mem_m.htm
> http://www.lispworks.com/documentation/HyperSpec/Body/f_pos_p.htm
>
> I'm open to variants of either or both of these, and have left the
I'd like to have both. These are very useful functions and if their
addition wouldn't be in conflict with any other aspects of Clojure, then
why not include them?
--
Dimitre Liotev
I understand that it seems less useful for vectors than what you had
hoped it meant, but it is still a useful in-range test.
What if I were to add sparse vectors?
Rich
Continuing down the ugly path I started, those *wouldn't* throw an
exception. :-/
I am going to cease and desist now, and accept contains? as it is. But
somebody is going to need to write a book to explain these odd corners
to people coming from the Java world. Bwa ha ha ha ha. :-)
Stuart
The fact that contains? is semantically different than
java.util.Collection#contains is confusing. If contains? was called
contains-key?, that would be more intuitive and map to how it works in
Java.