Reputation: 2850
The following code does not run as I am expecting. I want to be able to identify if foo and bar are empty and respond with an err accordingly else output "hello". However response only occurs when both foo and bar are present.
I'm new to Clojure and so probably missing something.
(defn create-entry [doc]
(let [id (str (java.util.UUID/randomUUID)) timestamp (quot (System/currentTimeMillis) 1000)]
(let [entry (assoc doc "id" id "timestamp" timestamp)]
(if (and (empty? [(get entry "foo") (empty? (get entry "bar")) ])
(response {:err "either foo or bar is required"})
) (prn "hello!")))))
Upvotes: 0
Views: 104
Reputation: 310
I know this has been answered, but I thought I would add something here too. This could be better by splitting this up into two functions, one predicate, and call that from your create-entry
function.
(defn entry? [entry]
(let [foo (get entry "foo")
bar (get entry "bar")]
(and (some? foo)
(some? bar))))
(defn create-entry [doc]
(let [id (str (UUID/randomUUID)) timestamp (quot (System/currentTimeMillis) 1000)]
(let [entry (assoc doc "id" id "timestamp" timestamp)]
(if (entry? entry)
(prn "hello!")
(response {:err "either foo or bar is required"})))))
Upvotes: 0
Reputation: 45826
You have some weird bracing issues going on. You stick both calls to get
inside a vector, then check if that hard coded vector is empty.
I think you meant for your condition to be something more like:
(and (empty? (get entry "foo"))
(empty? (get entry "bar")))
Upvotes: 1