Contains? is a predicate function that returns true
if the provided collection contains the provided key.
Its simplest use case is checking if a hash set contains a value.
(contains?
#{"Ann" "John" "Matt"}
"Matt")
=> true
Or if a hash map contains a certain key.
(contains?
{:name "Peter" :surname "Johnson"}
:name)
=> true
Contains? can also be used with any keyed sequence, like vectors.
(contains?
[1 3 5]
1)
=> true
However, it is important to remember that contains? cares about keys or indexes, not values. So in this example, even though 5 is a value in the vector, the vector only has 3 indexes namely 0, 1 and 2.
(contains?
[1 3 5]
5)
=> false
The same goes for strings.
(contains? "abcde" 2)
=> true
(contains? "abcde" \a)
=> IllegalArgumentException
Contains? can also be used for Java arrays, however not for Clojure lists or queues as these are not keyed sequences.
(contains?
(list "a" "b" "c")
2)
=> IllegalArgumentException