chapter six

6 Comparison and Equality

 

Equality in Clojure takes into account several aspects when comparing "objects" [115]:

  • Their values. Value is Clojure main equality ingredient. One consequence is that collections don’t necessarily have to be of the same type to be equal (provided that the content is the same). 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 also a very relevant aspect for comparison. Types are used for example to add semantic meaning to equality in case of ordered collections. Numbers for instance have a dedicated equality operator == (double equal) that removes some artificial behavior that = (single equal sign function) introduces in case of numbers.
  • 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  < (less than), > (more than), <= (less than or equal) ( and >= (more than or equal)

6.4  compare

6.5  identical?

6.6  hash

6.7  clojure.data/diff