boolean

added
1.0

ns
clojure.core

type
function

(boolean x)

Coerce to boolean

                ;; Everything except `false' and `nil' is true in boolean context.
user=> (into {} (map #(vector % (boolean %)) [true false nil [] {} '() #{} ""]))
{true true, false false, nil false, [] true, {} true, #{} true, "" true}

user=> (clojure.pprint/pp)
{true true,
 false false,
 nil false,
 [] true,
 {} true,
 #{} true,
 "" true}
nil

;; Unrelated to `boolean`, but notice that the `'()` entry is missing. This
;; due to Clojure treating `[]` and `'()` as equal. When combined into the map 
;; the `'() => true` key-value pair is overwritten by the `[] => true` key-value
;; pair. See https://github.com/zk/clojuredocs/issues/114#issuecomment-132153637

            
                ;; Beware that boolean returns true for numbers 0 and 1
user=> (boolean 0)
true
user=> (boolean 1)
true
            
                ;; Simply defined: Everything except false and nil is logically true in Clojure.
            
                ;; Corrected definition:
;; returns `false' for `false', `nil' and `(Boolean. false)`.
;; returns `true' for everything else.