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:
It is as simple as using a set - similar to maps, you can just drop it in the function position. It evaluates to the value if in the set (which is truthy) or nil
(which is falsey):
(#{100 101 102} 101) ; 101
(#{100 101 102} 99) ; nil
If you're checking against a reasonably sized vector/list you won't have until runtime, you can also use the set
function:
; (def nums '(100 101 102))
((set nums) 101) ; 101