=和==在Clojure [英] = and == in Clojure

查看:143
本文介绍了=和==在Clojure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在REPL上,如果我定义

On REPL, if I define

(def fits (map vector (take 10 (iterate inc 0))))

然后调用

(== [2] (nth fits 2))

(= [2] (nth fits 2))

返回true。

这个预期?我试过(类[2])和(类(nth适合2),并且都返回持久向量。

Is this expected ? I tried (class [2]) and (class (nth fits 2) and both return Persistent Vector.

推荐答案

== 用于比较数字。如果其中任何一个参数不是数字,它总是返回false:

== is for comparing numbers. If either of its arguments is not a number, it will always return false:

(== :a :a)
; => false

正如你可以看到,在REPL处有(clojure.contrib.repl-utils / source ==) repl-utils 'd,当然), == equiv 的方法clojure.lang.Numbers clojure / lang / Numbers.java (从GitHub的最新或接近最新的提交):

As you can see by saying (clojure.contrib.repl-utils/source ==) at the REPL (with repl-utils require'd, of course), == calls the equiv method of clojure.lang.Numbers. The relevant bit of clojure/lang/Numbers.java (from the latest or close-to-latest commit on GitHub):

static public boolean equiv(Object x, Object y){
    return y instanceof Number && x instanceof Number
           && equiv((Number) x, (Number) y);
}

使用 = 可以不等于数字的平等比较。当你事实上处理数字时, == 应该有点快。

Use = for equality comparisons of things which may not be numbers. When you are in fact dealing with numbers, == ought to be somewhat faster.

这篇关于=和==在Clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆