Test whether a list contains a specific value in Clojure

后端 未结 18 2249
自闭症患者
自闭症患者 2020-11-30 17:17

What is the best way to test whether a list contains a given value in Clojure?

In particular, the behaviour of contains? is currently confusing me:

18条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 17:57

    The recommended way is to use some with a set - see documentation for clojure.core/some.

    You could then use some within a real true/false predicate, e.g.

    (defn in? [coll x] (if (some #{x} coll) true false))
    

提交回复
热议问题