liveBook · Manning

Boolean operation

As explained in Clojure: The Essential Reference

A boolean operator is an operator or function that combines or transforms truth-valued expressions.

A boolean operation combines or transforms truth-valued expressions. Clojure provides short-circuiting operators and the single-argument not operator, which reverses boolean meaning. section 3.2

Clojure treats only false and nil as false; every other value is truthy, including 0, the empty string, and the empty list. Predicates may therefore return any type when used by collection logic. section 3.2 section 8.1.5

Collection functions turn boolean operations into practical questions:

  • Use every? to ask whether a predicate succeeds for every element; use not-every? for its negation. section 8.1.5
  • Use some to find the first non-nil predicate result; use not-any? to test that no element succeeds. section 8.1.5
  • These functions accept seq-compatible collections, including standard collections, arrays, and Java collection types; unsupported inputs raise IllegalArgumentException. section 8.1.5

Testing collections and locating a match

(every? pos? [1 2 3 4]) ;; true (some #(when (= "222 231 9008" (:phone %)) %) address-book) ;; {:phone "222 231 9008" :name "Sarah"}

Table 3.1. Common Clojure values and their boolean translation

Description

Example

Boolean Value

Number zero

0

true

Empty string

""

true

Empty list

()

true

List of nil

(nil)

true

Nil

nil

false

Compare representative Clojure values with their boolean translations, including nil, zero, empty strings, and empty lists. section 3.2

How Clojure: The Essential Reference covers it

How deep it goes
Explains truthiness, predicate contracts, return behavior, accepted collections, and one input exception, but defers comparison details.
Examples
Demonstrates positivity tests, negative-number searches, address-book lookup, bingo-card validation, and prize-ticket matching.
What you can do after
Apply boolean logic through Clojure operators and collection functions to test conditions or retrieve the first matching result.
How it connects
Connects boolean operations with predicates, truth values, collections, comparison, identity, and bitwise operators.

Also explained in

Compare how 4 books explain boolean operation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.