Test whether a list contains a specific value in Clojure

后端 未结 18 2307
自闭症患者
自闭症患者 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 18:11

    Since Clojure is built on Java, you can just as easily call the .indexOf Java function. This function returns the index of any element in a collection, and if it can't find this element, returns -1.

    Making use of this we could simply say:

    (not= (.indexOf [1 2 3 4] 3) -1)
    => true
    

提交回复
热议问题