clojure - Clojure 相同吗?如果被比较的东西实际上是同一个实例,函数只返回 true 吗?

标签 clojure

我以为

(identical? x y)

仅当 x 和 y 是同一个实例时才返回 true?那么这个呢:

(def moo 4)
(def cow 4)

(identical? moo cow)
true

但我认为 moo 和 cow 都是整数“4”的单独实例?是什么赋予了?

最佳答案

在 JVM 中,-128127 之间的两个相等的整数始终相同,因为它维护 IntegerCache .

这意味着 -128 和 127 之间的两个相等的整数始终是 Integer 类的同一个实例。

尝试比较不同的整数:

(identical? 4 (+ 2 2)) ; true
(identical? 127 127) ; true
(identical? 128 128) ; false

参见this answer on Code Golf了解更多信息。

关于clojure - Clojure 相同吗?如果被比较的东西实际上是同一个实例,函数只返回 true 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25832615/

相关文章:

database - clojure.java.jdbc 中缺少必需的参数

clojure - 响应图为零

clojure - Clojure 树函数中的子树总计

clojure - Clojure 中是否有一个标准函数接受一个参数,忽略它并返回一个常量?

clojure - 无法在 incanter 1.4.0 中创建矩阵

clojure - 在不定义宏的情况下将宏应用于窗体

data-structures - clojure 中是否有内置数据结构支持重复元素和 O(1) 删除?

clojure - 使用 let 子句中提供的 midje 不会 stub 方法

intellij-idea - 如何在 Cursive IDE 中启用彩虹括号?

Clojure:在 Java 对象上调用一系列方法