[CLJ-877] contains? is broken for vectors Created: 14/Nov/11 Updated: 14/Nov/11 Resolved: 14/Nov/11
|Affects Version/s:||Release 1.3|
contains? returns the wrong result for the last item in a vector:
user> (map #(contains? [1 2 3 4] %) [1 2 3 4])
|Comment by Steve Miner [ 14/Nov/11 12:41 PM ]|
This is not a bug. Check the doc – contains? refers to the keys of the collection, not the values. For a vector, the "keys" are the indices.
user=> (map #(contains? [10 20 30 40] %) (range 4))
|Comment by Ben Smith-Mannschott [ 14/Nov/11 12:45 PM ]|
This is a (common) misusing of contains?. Perhaps it would have been better if contains? had been named contains-key?, but that ship has sailed.
A vector of length 4 has four keys (indexes): 0, 1, 2, 3, which is why your example is returning (true true true false).
To get the behavior your are expecting from contains?, use some:
If you prefer booleans, you could do something like this: