Reputation: 161
Scheme supports boolean?
to test whether a symbol or value is of boolean type.
(boolean? #\t)
(boolean? #\f)
While in Clojure, I can only found integer?
, number?
, list?
, etc but without boolean?
.
What is the equivalent of boolean?
in Clojure?
Upvotes: 4
Views: 3487
Reputation: 5392
Since Clojure 1.9 there is a boolean?
predicate function in clojure.core
.
Upvotes: 4
Reputation: 821
Yet another version:
(defn boolean? [x]
(or (true? x) (false? x)))
Upvotes: 8
Reputation: 13473
if
form and all its progeny. In that sense, everything is boolean. nil
and false
itself. ThoughBoolean/FALSE
, a static object containing false
, and (Boolean.
false)
- a new Boolean
object containing false
- evaluate
false. This last does not accord with how I read the documentation, but that's
what Light Table is showing me. I'd say steer clear of constructing your
own Boolean
s. Why Java allows it, I can't fathom. #{true false}
. If you need to do so,
follow Shlomi's advice. nil
for nothing or failure. For
example, a set applied to a non-member. In such cases, you may find
yourself using nil?
to test for nil
, particularly where false
would be a valid and distinct value.Upvotes: 4
Reputation: 9886
If you're in the land of types, and want to know if something is a boolean, then as Shlomi has posted you can make a boolean?
function easily enough from instance?
. Here's a varargs version:
(defn bools?
[& xs]
(every? (partial instance? Boolean) xs))
with outputs:
>> (bools? true) => true
>> (bools? true false) => true
>> (bools? true nil) => false
I've never needed to do this, as I've only ever dealt with values and the fact that everything in clojure is "truthy" except for false or nil.
Upvotes: 1