I came across this snippet of code in a book of Clojure. Can you please explain me how contains?
works?
(contains? [1 2 3] 3)
;= false
(contains? [1 2 3] 2)
;= true
(contains? [1 2 3] 0)
;= true
I came across this snippet of code in a book of Clojure. Can you please explain me how contains?
works?
(contains? [1 2 3] 3)
;= false
(contains? [1 2 3] 2)
;= true
(contains? [1 2 3] 0)
;= true
Just look at the documentation:
contains?
(contains? coll key)
Returns true if key is present in the given collection, otherwise returns false. Note that for numerically indexed collections like vectors and Java arrays, this tests if the numeric key is within the range of indexes. 'contains?' operates constant or logarithmic time; it will not perform a linear search for a value. See also 'some'.
So, in your example, (contains? [1 2 3] 3)
returns false
because the collection [1 2 3]
does not contain an item at the index 3
(that means (get [1 2 3] 3)
would return nil
).