ClojureDocs

Nav

Namespaces

=

clojure.core

Available since 1.0 (source)
  • (= x)
  • (= x y)
  • (= x y & more)
Equality. Returns true if x equals y, false if not. Same as
Java x.equals(y) except it also works for nil, and compares
numbers and collections in a type-independent manner.  Clojure's immutable data
structures define equals() (and thus =) as a value, not an identity,
comparison.
7 Examples
user=> (= 1)
true
user=> (= 1 1)
true
user=> (= 1 2)
false
user=> (= 1 1 1)
true
user=> (= 1 1 2)
false
user=> (= '(1 2) [1 2])
true
user=> (= nil nil)
true
user=> (= (sorted-set 2 1) (sorted-set 1 2))
true

;; It should be noted that equality is not defined for Java arrays.
;; Instead you can convert them into sequences and compare them that way.
;; (= (seq array1) (seq array2))
;; There are functional differences between = and == 
;; = may introduce java autoboxing 

;; true:
(= 1)
(= 1 1)       
(= 1/1, 2/2, 3/3, 4/4)   
(= :foo)
(= nil anything)     ; anything = nil


;; false:
(= 1, 1.0, 1/1) ; differs from ==
(= 1 2)
(= 1 \1)        ; differs from ==
(= 1 "1")       ; differs from ==

;; If passed a single value (= x) the result is always true.
(= 1)
(= nil)
(= false)
(= true)
(= {:a 1 :b2})
(= 'false)
;;=> true  
;; = can be used to compare the equality of nested Clojure data structures
(= {:a [1 {1 2}] :b 'ok :c "string"} 
   {:b 'ok :c "string" :a '(1 {1 2})})
;;=> true
;; See the Clojure Equality guide for more details:
;; https://clojure.org/guides/equality

;; One perhaps surprising case is comparing regular expressions.
;; In Clojure, regular expressions only equal one another if they are
;; the same object in memory, i.e. if (identical? x y) is also true.
;; Therefore, any data structure containing regular expression will 
;; only be equal to one another if the corresponding regular expressions are
;; identical.
;; See  https://dev.clojure.org/jira/browse/CLJ-1182
(= #"fav*" #"fav*")
;;=> false

;; Another exception is the special floating point value ##NaN for "Not a Number".
;; In Clojure on Java, this value is never equal to itself, and any collections
;; containing such "values" are never equal to any other collections, either:
(= ##NaN ##NaN)
;;=> false

(= [1 2 3 ##NaN] [1 2 3 ##NaN])
;;=> false

;; There are a few other minor exceptions where clojure.core/= might behave in
;; ways that surprise you.  You can find an article describing the known cases
;; for Clojure on Java here:  https://clojure.org/guides/equality
;; You can test for a Java infinity, but there are quirks to know about.  

;; Note particularly that Double/POSITIVE_INFINITY and Float/POSITIVE_INFINITY
;; are = and ==, but they are neither identical? nor .equals.  However, the
;; value these Java objects return has the same representation, ##Inf, in
;; Clojure.  Further, the literal ##Inf is parsed as something that is
;; .equals to Double/POSITIVE_INFINITY but not identical? to it.  Similar
;; points hold for the Java NEGATIVE_INFINITY's and their Clojure
;; representation, ##-Inf.

user=> Double/POSITIVE_INFINITY
;;=> ##Inf
user=> Float/POSITIVE_INFINITY
;;=> ##Inf
user=> (= Double/POSITIVE_INFINITY Float/POSITIVE_INFINITY)
;;=> true
user=> (= ##Inf ##Inf)
;;=> true
user=> (== Double/POSITIVE_INFINITY Float/POSITIVE_INFINITY)
;;=> true
user=> (== ##Inf ##Inf)
;;=> true
user=> (identical? Double/POSITIVE_INFINITY Float/POSITIVE_INFINITY)
;;=> false
user=> (identical? ##Inf ##Inf)
;;=> true
user=> (.equals Double/POSITIVE_INFINITY Float/POSITIVE_INFINITY)
;;=> false
user=> (.equals ##Inf ##Inf)
;;=> true
user=> (identical? ##Inf Double/POSITIVE_INFINITY)
;;=> false
user=> (identical? ##Inf Float/POSITIVE_INFINITY)
;;=> false
user=> (.equals ##Inf Double/POSITIVE_INFINITY)
;;=> true
user=> (.equals ##Inf Float/POSITIVE_INFINITY)
;;=> false

;; (Tested with Clojure 1.10.3.)
user=> (= + +)
true
user=> (= = =)
true
user=> (= inc inc)
true
user=> (= #() #())
false
See Also

Returns non-nil if nums all have the equivalent value (type-independent), otherwise false

Added by gstamp

Same as (not (= obj1 obj2))

Added by alimoeeny

Tests if 2 arguments are the same object

Added by TimMc
2 Notes
    By , created 14.5 years ago

    There is a difference between "=" and "==". For primitives you definitely want to use "==" as "=" will result in a cast to the wrapped types for it's arguments.

    This may not be the case come Clojure 1.3 (see [1])

    [1] http://github.com/clojure/clojure/commit/df8c65a286e90e93972bb69392bc106128427dde

    By , created 2.2 years ago

    = compares collections in a type-independent manner but not numbers, despite what the docs say. See examples above.