6 Comparison and equality

 

Equality in Clojure takes into account several aspects when comparing "objects" ("object" here is intended in the most general meaning, without relationship to Object Oriented Programming):

  • Their values. The "value" of an object is Clojure’s equality main ingredient. One consequence is that collections don’t necessarily need to have the same type to be equal. Values satisfy the common intuition that (1 2) and [1 2] are equal, despite the fact that clojure.lang.PersistentList and clojure.lang.PersistentVector are not the same type. The equality operator = is mainly based on this principle.
  • Their types. Types are definitely a relevant aspect for comparison. Types are used for example to add semantic meaning to equality in case of ordered collections.
  • Their identity. Finally, Clojure also offers a way to check if two objects are the same instance (which most of the time resolves into checking if they have the same address in memory) with identical?.

6.1 = (equal) and not= (not equal)

6.2 == (double equal)

6.3 < , > , <= , >=

6.4 compare

6.5 identical?

6.6 hash

6.7 clojure.data/diff

6.8 Summary