concept equality in category clojure

appears as: equality, Equality, equality
Clojure: The Essential Reference

This is an excerpt from Manning's book Clojure: The Essential Reference.

Conveniently, the count-occurrences function in the example can handle any item type, not just "words" (provided items contains some definition of equality that can be used to store them in the hash-map). Even more conveniently, Clojure contains such a function in the standard library already, it’s called “frequencies”:

Clojure equality is designed to be intuitive most of the time (without requiring a specific knowledge of types) with a few corner cases that are going to be illustrated in this chapter. Compared to Object Oriented Programming, functional programming puts less emphasis on container objects and their need for a custom implementation of equality (and hashing as a consequence). Clojure, on the other hand, offers analogous custom data structures called records (created with “defrecord”) which come with a built-in equality based on attributes.

Equality accepts any number of arguments, not just two. We could use a variable number of arguments in the following simulation. In the classic slot-machine gambling game, three (or more) reels come to a stop after spinning for some time. The user wins if the reels are aligned showing the same symbol. Here’s how a simulated slot-machine could be implemented in Clojure:

(defn generate [& [{:keys [cheat reels]
                    :or {cheat 0 reels 3}}]] ; #1
  (->> (repeatedly rand)                     ; #2
       (map #(int (* % 100)))                ; #3
       (filter pos?)                         ; #4
       (map #(mod (- 100 cheat) %))          ; #5
       (take reels)))                        ; #6

(defn play [& [opts]]
  (let [res (generate opts)]
    {:win (apply = res)                      ; #7
     :result res}))

(play)
;; {:win false, :result (12 9 19)}

(play {:cheat 100})
;; {:win true, :result (0 0 0)}

(play {:reels 10})
;; {:win false, :result (38 29 31 2 16 7 14 4 46 40)}

What is so difficult about equality?

While it is often intuitive to call two things "the same" in spoken language, it is more difficult to find a formal definition in programming. Humans often infer the meaning of equality based on context or other aspect of the conversation, adjusting precision as a consequence. In computing equality involves a (potentially expensive) bit by bit comparison. What happens in most modern languages though, is that there is an agreed set of types that can skip complete byte comparison.

The Joy of Clojure, Second Edition

This is an excerpt from Manning's book The Joy of Clojure, Second Edition.

This is because the comparison function used by the sorted map determines order by equality, and if two keys compare as equal, only one is kept. This applies to comparison functions provided to sorted-map-by as well as the default comparator shown previously.

Equality has meaning

Equality in the presence of mutability has no meaning. Equality in the face of mutability and concurrency is utter lunacy. If any two objects resolve as being equal now, then there’s no guarantee that they will a moment from now. And if two objects aren’t equal forever, then they’re technically never equal (Baker 1993). Providing immutable objects once again assigns meaning to equality, in that if two objects are equal now, they’ll always be so.