If you have a vector or list and want to check whether a value is contained in it, you will find that contains? does not work.
Michał has already explained why explained why.
; does not work as you might expect
(contains? [:a :b :c] :b) ; = false
There are four things you can try in this case:
Consider whether you really need a vector or list. If you use a set instead,
contains?will work.(contains? #{:a :b :c} :b) ; = trueUse
some, wrapping the target in a set, as follows:(some #{:b} [:a :b :c]) ; = :b, which is truthyThe set-as-function shortcut will not work if you are searching for a falsy value (
falseornil).; will not work (some #{false} [true false true]) ; = nilIn these cases, you should use the built-in predicate function for that value,
false?ornil?:(some false? [true false true]) ; = trueIf you will need to do this kind of search a lot, write a function for it:
(defn seq-contains? [coll target] (some #(= target %) coll)) (seq-contains? [true false true] false) ; = true
Also, see Michał’s answer Michał’s answer for ways to check whether any of multiple targets are contained in a sequence.
If you have a vector or list and want to check whether a value is contained in it, you will find that contains? does not work.
Michał has already explained why.
; does not work as you might expect
(contains? [:a :b :c] :b) ; = false
There are four things you can try in this case:
Consider whether you really need a vector or list. If you use a set instead,
contains?will work.(contains? #{:a :b :c} :b) ; = trueUse
some, wrapping the target in a set, as follows:(some #{:b} [:a :b :c]) ; = :b, which is truthyThe set-as-function shortcut will not work if you are searching for a falsy value (
falseornil).; will not work (some #{false} [true false true]) ; = nilIn these cases, you should use the built-in predicate function for that value,
false?ornil?:(some false? [true false true]) ; = trueIf you will need to do this kind of search a lot, write a function for it:
(defn seq-contains? [coll target] (some #(= target %) coll)) (seq-contains? [true false true] false) ; = true
Also, see Michał’s answer for ways to check whether any of multiple targets are contained in a sequence.
If you have a vector or list and want to check whether a value is contained in it, you will find that contains? does not work.
Michał has already explained why.
; does not work as you might expect
(contains? [:a :b :c] :b) ; = false
There are four things you can try in this case:
Consider whether you really need a vector or list. If you use a set instead,
contains?will work.(contains? #{:a :b :c} :b) ; = trueUse
some, wrapping the target in a set, as follows:(some #{:b} [:a :b :c]) ; = :b, which is truthyThe set-as-function shortcut will not work if you are searching for a falsy value (
falseornil).; will not work (some #{false} [true false true]) ; = nilIn these cases, you should use the built-in predicate function for that value,
false?ornil?:(some false? [true false true]) ; = trueIf you will need to do this kind of search a lot, write a function for it:
(defn seq-contains? [coll target] (some #(= target %) coll)) (seq-contains? [true false true] false) ; = true
Also, see Michał’s answer for ways to check whether any of multiple targets are contained in a sequence.
- 30.7k
- 11
- 101
- 133
If you have a vector or list and want to check whether a value is contained in it, you will find that contains? does not work.
Michał has already explained why.
; does not work as you might expect
(contains? [:a :b :c] :b) ; = false
There are four things you can try in this case:
Consider whether you really need a vector or list. If you use a set insteaduse a set instead,
contains?will work.(contains? #{:a :b :c} :b) ; = trueUse
someinsteadUsesome, wrapping the target in a set, as follows:(some #{:b} [:a :b :c]) ; = :b, which is truthyThe set-as-function shortcut will not work if you are searching for a falsy value (
falseornil).; will not work (some #{false} [true false true]) ; = nilIn these cases, you should use the built-in predicate functionuse the built-in predicate function for that value,
false?ornil?:(some false? [true false true]) ; = trueIf you will need to do this kind of search a lot, write a function for itwrite a function for it:
(defn seq-contains? [coll target] (some #(= target %) coll)) (seq-contains? [true false true] false) ; = true
Also, see Michał’s answer for ways to check whether any of multiple targets are contained in a sequence.
If you have a vector or list and want to check whether a value is contained in it, you will find that contains? does not work.
Michał has already explained why.
; does not work as you might expect
(contains? [:a :b :c] :b) ; = false
There are four things you can try in this case:
Consider whether you really need a vector or list. If you use a set instead,
contains?will work.(contains? #{:a :b :c} :b) ; = trueUse
someinstead, wrapping the target in a set, as follows:(some #{:b} [:a :b :c]) ; = :b, which is truthyThe set-as-function shortcut will not work if you are searching for a falsy value (
falseornil).; will not work (some #{false} [true false true]) ; = nilIn these cases, you should use the built-in predicate function for that value,
false?ornil?:(some false? [true false true]) ; = trueIf you will need to do this kind of search a lot, write a function for it:
(defn seq-contains? [coll target] (some #(= target %) coll)) (seq-contains? [true false true] false) ; = true
Also, see Michał’s answer for ways to check whether any of multiple targets are contained in a sequence.
If you have a vector or list and want to check whether a value is contained in it, you will find that contains? does not work.
Michał has already explained why.
; does not work as you might expect
(contains? [:a :b :c] :b) ; = false
There are four things you can try in this case:
Consider whether you really need a vector or list. If you use a set instead,
contains?will work.(contains? #{:a :b :c} :b) ; = trueUse
some, wrapping the target in a set, as follows:(some #{:b} [:a :b :c]) ; = :b, which is truthyThe set-as-function shortcut will not work if you are searching for a falsy value (
falseornil).; will not work (some #{false} [true false true]) ; = nilIn these cases, you should use the built-in predicate function for that value,
false?ornil?:(some false? [true false true]) ; = trueIf you will need to do this kind of search a lot, write a function for it:
(defn seq-contains? [coll target] (some #(= target %) coll)) (seq-contains? [true false true] false) ; = true
Also, see Michał’s answer for ways to check whether any of multiple targets are contained in a sequence.
- 30.7k
- 11
- 101
- 133
If you have a vector or list and want to check whether a value is contained in it, you will find that contains? does not work.
Michał has already explained why.
; does not work as you might expect
(contains? [:a :b :c] :b) ; = false
There are four things you can try in this case:
Consider whether you really need a vector or list. If you use a set instead,
contains?will work.(contains? #{:a :b :c} :b) ; = trueUse
someinstead, wrapping the target in a set, as follows:(some #{:b} [:a :b :c]) ; = :b, which is truthyThe set-as-function shortcut will not work if you might beare searching for a falsy value (
falseornil).; will not work (some #{false} [true false true]) ; = nilIn that casethese cases, you will have to writeshould use the built-in predicate function the long wayfor that value,
false?ornil?:(some #(= false %)? [true false true]) ; = trueIf you will need to do this kind of search a lot, write a function for it:
(defn seq-contains? [coll target] (some #(= target %) coll)) (seq-contains? [true false true] false) ; = true
Also, see Michał’s answer for a wayways to check whether any of multiple targets are contained in a sequence.
If you have a vector or list and want to check whether a value is contained in it, you will find that contains? does not work.
Michał has already explained why.
; does not work as you might expect
(contains? [:a :b :c] :b) ; = false
There are four things you can try in this case:
Consider whether you really need a vector or list. If you use a set instead,
contains?will work.(contains? #{:a :b :c} :b) ; = trueUse
someinstead, wrapping the target in a set, as follows:(some #{:b} [:a :b :c]) ; = :b, which is truthyThe set-as-function shortcut will not work if you might be searching for a falsy value (
falseornil).; will not work (some #{false} [true false true]) ; = nilIn that case, you will have to write the predicate function the long way:
(some #(= false %) [true false true]) ; = trueIf you will need to do this kind of search a lot, write a function for it:
(defn seq-contains? [coll target] (some #(= target %) coll)) (seq-contains? [true false true] false) ; = true
Also, see Michał’s answer for a way to check whether any of multiple targets are contained in a sequence.
If you have a vector or list and want to check whether a value is contained in it, you will find that contains? does not work.
Michał has already explained why.
; does not work as you might expect
(contains? [:a :b :c] :b) ; = false
There are four things you can try in this case:
Consider whether you really need a vector or list. If you use a set instead,
contains?will work.(contains? #{:a :b :c} :b) ; = trueUse
someinstead, wrapping the target in a set, as follows:(some #{:b} [:a :b :c]) ; = :b, which is truthyThe set-as-function shortcut will not work if you are searching for a falsy value (
falseornil).; will not work (some #{false} [true false true]) ; = nilIn these cases, you should use the built-in predicate function for that value,
false?ornil?:(some false? [true false true]) ; = trueIf you will need to do this kind of search a lot, write a function for it:
(defn seq-contains? [coll target] (some #(= target %) coll)) (seq-contains? [true false true] false) ; = true
Also, see Michał’s answer for ways to check whether any of multiple targets are contained in a sequence.
- 30.7k
- 11
- 101
- 133