What's That Function?

λ?

contains? clojure.core

Contains? is a predicate function that returns true if the provided collection contains the provided key.

Example 1

Its simplest use case is checking if a hash set contains a value.

(contains?
  #{"Ann" "John" "Matt"}
  "Matt")

=> true

Example 2

Or if a hash map contains a certain key.

(contains?
  {:name "Peter" :surname "Johnson"}
  :name)

=> true

Example 3

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

Example 4

The same goes for strings.

(contains? "abcde" 2)

=> true
(contains? "abcde" \a)

=> IllegalArgumentException

Example 5

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