4

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?

1
  • 3
    Keep in mind that true is not the only true value, and falseis not the only false value. You probably know that. This is just a warning to newbies reading this later.
    – Mars
    Commented Apr 12, 2015 at 21:28

6 Answers 6

10

you could do

(defn boolean? [x]
  (instance? Boolean x))
8

Yet another version:

(defn boolean? [x]
  (or (true? x) (false? x)))
3
  • Is there a value of x that can make this function return false? I would think no matter what you give it, it will return true. false is falsey, null is falsey and everything else you could give it is truthy. Commented Feb 11, 2016 at 1:56
  • 3
    Every value except for Boolean.TRUE and Boolean.FALSE make this function return false. Read the docs of true? and false?. They're exactly for distinguishing truthiness from real boolean values. Commented Feb 11, 2016 at 5:21
  • 1
    That's great, and works for Clojurescript too (I just tested in cljs and clj REPLs). Commented Feb 11, 2016 at 5:28
4
  • In Clojure, all values are logical: valid first arguments to an if form and all its progeny. In that sense, everything is boolean.
  • The only false values are nil and false itself. Though
    Boolean/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 Booleans. Why Java allows it, I can't fathom.
  • I've never come across a case where the logic required knowing whether something was in the set #{true false}. If you need to do so, follow Shlomi's advice.
  • Many standard functions produce 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.
1
  • While in general the need for this doesn't seem valid, one case where one would want to do this is if a command-line option such as a debug flag was to be input to the program. In such a case the coder would want to ensure that the user clearly set a true value and did not provide some non-false value by accident.
    – Don
    Commented Jan 4, 2016 at 17:17
4

Since Clojure 1.9 there is a boolean? predicate function in clojure.core.

1

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.

1

(defn bool? [x] (= x (boolean x)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.