3
votes

Is there any function in clojure which calculates the boolean value of the given expression? What I mean is if expression is nil or false then this function returns false, otherwise true?

(to-bool nil) ; => false
(to-bool false) ; => false
(to-bool java.util.Date.) ; => true
(to-bool true) ; => true
(to-bool 7) ; => true

Something like that?

2

2 Answers

7
votes

boolean it is.

(boolean nil) ; => false
(boolean false) ; => false  (I assume you expect false, not true here!)
(boolean (java.util.Date.)) ; => true
(boolean true) ; => true
(boolean 7) ; => true
-2
votes

there are two logical false in clojure only: false, nil.

user=> (if false :true :false)
:false
user=> (if nil :true :false)
:false
user=> (if [] :true :false)
:true
user=> (if () :true :false)
:true
user=> (if #{} :true :false)
:true
user=> (if 1 :true :false)
:true
user=> (if 0 :true :false)
:true
user=> (if (java.util.Date.) :true :false)
:true