Boolean type and related logic operations.

Dunaj (and Clojure) performs an automatic coercion to Boolean when e.g. calling a function which expects a boolean argument. By definition, nil and false coerce to false, any other value coerces to true.

JVM host specific: Dunaj supports primitive booleans as function arguments and return values (subject to the number of arguments and combination of argument types).

JVM host specific: false is identical to Boolean/FALSE, so using other instances of java.lang.Boolean may lead to surprising results. In such cases, you may want to use boolean function to coerce into correct boolean value.

1
2
3
4
5
(if (java.lang.Boolean. false) :is-true :is-false)
;;=> :is-true

(if (boolean (java.lang.Boolean. false)) :is-true :is-false)
;;=> :is-false

Primary

Boolean

Available since version 1.0 (view source)

TYPE

Extends: IComparable

A boolean type, which has two values, true and false.

JVM host specific: false is identical to Boolean/FALSE and true is identical to Boolean/TRUE

This type is not available in Dunaj lite, please use Boolean+ instead.

Boolean+

Available since version 1.0 (view source)

TYPE

Extends: IComparable

A boolean type, which has two values, true and false.

Identical to Boolean type, meant to be used in Dunaj lite.

JVM host specific: false is identical to Boolean/FALSE and true is identical to Boolean/TRUE

See also: boolean, false?, true?, Boolean

boolean

Available since version 1.0 (alias of clojure.core/boolean)

Usage:
  • (boolean x)

Type signature:
  • (Any) → Boolean

Returns x coerced to Boolean type, by returning false if x is nil or false value, and returning true for all other values. As a special case, boolean returns false also if given instance of host boolean type which represents a false value.

1
2
3
4
5
6
7
8
9
10
11
(boolean :foo)
;;=> true

(boolean nil)
;;=> false

(boolean false)
;;=> false

(boolean (java.lang.Boolean. false))
;;=> false
1
2
3
4
5
6
7
8
9
10
11
(false? false)
;;=> true

(false? java.lang.Boolean/FALSE)
;;=> true

(false? (java.lang.Boolean. false))
;;=> false

(false? (boolean (java.lang.Boolean. false)))
;;=> true

See also: Boolean+, boolean?, true?, false?

boolean?

Available since version 1.0 (view source)

Usage:
  • (boolean? x)

Type signature: Predicate

Returns true if object x is an instance of Boolean type, false otherwise.

false?

Available since version 1.0 (alias of clojure.core/false?)

Usage:
  • (false? x)

Type signature: Predicate

Returns true if x is false, otherwise returns false.

false? returns false if x is nil.
1
2
3
4
5
6
7
8
9
10
11
(false? false)
;;=> true

(false? nil)
;;=> false

(false? (java.lang.Boolean. false))
;;=> false

(false? (boolean (java.lang.Boolean. false)))
;;=> true

true?

Available since version 1.0 (alias of clojure.core/true?)

Usage:
  • (true? x)

Type signature: Predicate

Returns true if x is true, otherwise returns false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(true? true)
;;=> true

(true? :foo)
;;=> false

(true? java.lang.Boolean/TRUE)
;;=> true

(true? (java.lang.Boolean. true))
;;=> false

(true? (boolean (java.lang.Boolean. true)))
;;=> true

Logic

In this section: and implication nand nor not or xnor xor

and

Available since version 1.0 (alias of clojure.core/and)

MACRO (and)  (and x)  (and x & next)

Evaluates given expressions one at a time, from left to right. If a form returns logical false (nil or false), and returns that value and doesn’t evaluate any of the other expressions, otherwise it returns the value of the last expression. (and) returns true.

Table 1. Truth table for and
x y (and x y)

false

false

false

false

true

false

true

false

false

true

true

true

implication

Available since version 1.0 (view source)

not referred automatically

MACRO (implication x y)

Returns the result of material implication. Note that implication is not supported for more than 2 arguments, as implication is not associative.

Table 2. Truth table for implication
x y (implication x y)

false

false

true

false

true

true

true

false

false

true

true

true

See also: dunaj.bit/and-not

nand

Available since version 1.0 (view source)

not referred automatically

MACRO (nand & body)

The logical nand operator. Returns the negation of and. Stops the evaluation of given expressions on first logical false value.

Table 3. Truth table for nand
x y (nand x y)

false

false

true

false

true

true

true

false

true

true

true

false

See also: and, nor

nor

Available since version 1.0 (view source)

not referred automatically

MACRO (nor & body)

The logical nor operator. Returns the negation of or. Stops the evaluation of given expressions on first logical true value.

Table 4. Truth table for nor
x y (nor x y)

false

false

true

false

true

false

true

false

false

true

true

false

See also: or, xor, xnor

not

Available since version 1.0 (alias of clojure.core/not)

Usage:
  • (not x)

Type signature:
  • (Any) → Boolean+

Returns true if x is logical false (nil or false), otherwise returns false.

Table 5. Truth table for not
x (not x)

false

false

true

true

or

Available since version 1.0 (alias of clojure.core/or)

MACRO (or)  (or x)  (or x & next)

Evaluates given expressions one at a time, from left to right. If a form returns a logical true value, or returns that value and doesn’t evaluate any of the other expressions, otherwise it returns the value of the last expression. (or) returns nil.

Table 6. Truth table for or
x y (or x y)

false

false

false

false

true

true

true

false

true

true

true

true

xnor

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (xnor)

  • (xnor x)

  • (xnor x y)

Type signatures:
  • () → Boolean+

  • (Any) → Boolean+

  • (Any ⨯ Any) → Boolean+

Returns the negation of xor. Note that xnor is not supported for more than 2 arguments, as logical biconditional is ambiguous in such cases. (xnor) returns true, (xnor x) is same as (not x).

Table 7. Truth table for xnor
x y (xnor x y)

false

false

true

false

true

false

true

false

false

true

true

true

See also: nor, or

xor

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (xor)

  • (xor x)

  • (xor x y)

  • (xor x y & more)

Type signatures:
  • () → Any

  • (Any) → Any

  • (Any ⨯ Any) → Any

  • (Any ⨯ Any ⨯ (Va Any)) → Any

Returns the last logical true value if odd number of inputs is logical true, otherwise returns a logical false value. (xor) returns nil, (xor x) returns x.

Table 8. Truth table for xor
x y (xor x y)

false

false

false

false

true

true

true

false

true

true

true

false

See also: or, nor, xnor