Reputation: 78549
I'm learning Clojure, and trying to figure out the language to write things concisely and clearly. I'm trying to conditionally do something with a value after getting a value (say from a database) from a function that either returns the value or nil. I'm currently using a let binding, and then an if statement. Here is something similar to what I have currently:
(defn foo [key]
(let [result (try-getting! key)]
(if result
(+ result 50)
50)))
I'm trying to figure out if there is a more consise way to do this, perhaps some sort of combined if-let binding? Is there a way to write this better?
Thanks!
Upvotes: 2
Views: 378
Reputation: 1576
There is form if-let for this:
(defn foo [key]
(if-let [result (try-getting! key)]
(+ result 50)
50))
Upvotes: 8
Reputation: 20194
Yes, in fact if-let
is what you want here.
(if-let [result (try-getting! key)]
(+ result 50)
50)
Upvotes: 5