Search code examples
clojure

Why does (condp contains? (:symbols xx) :a (prn "yes")) give contains? not supported on type: clojure.lang.Keyword error?


I want to use condp for a problem.

This is what I am trying to checking using condp.

(def xx {:symbols {:a 1}})

(contains? (:symbols xx) :a)

true

But I get this error

(condp contains? (:symbols xx) :a (prn "yes"))

IllegalArgumentException contains? not supported on type:     
clojure.lang.Keyword  clojure.lang.RT.contains

Solution

  • This is due to the order of arguments being passed to contains? by condp — it's passing the keyword as the first argument. If you create an anonymous function that swaps the argument order, it'll do what you want:

    user=> (def xx {:symbols #{:a :b}})
    user=> (condp #(contains? %2 %1) (:symbols xx) :a (prn "yes"))
    "yes"
    nil
    

    This is the relevant line from the condp doc string explaining that argument-order behavior:

    For each clause, (pred test-expr expr) is evaluated.