简体   繁体   English

clojure地图中的默认值

[英]Default value in clojure maps

I have a clojure map. 我有一张clojure地图。 Name it opts . 将其命名为opts I know 2 ways to get values from this map such that if there is no key get some default value: 我知道从这个地图获取值的两种方法,如果没有键获得一些默认值:

(let [opts {}
      title-1 (or (:title opts) "Default title")
      title-2 (:title opts "Default title")]
  (println title-1 title-2))

I saw some libraries (quil, incanter) use first approach with or . 我看到一些库(quil,incanter)使用or使用第一种方法。 It seems to me that second approach is more concise and cleaner. 在我看来,第二种方法更简洁,更清洁。 Is there advantages in the first approach? 第一种方法有优势吗?
Disadvantage of using (or (:title opts) "Default value") is that we cannot pass false and nil as values anymore, default value is always used in this case. 使用(or (:title opts) "Default value")缺点是我们不能再将falsenil作为值传递,在这种情况下总是使用默认值。

A crucial difference between (or (:key hash) default) and (:key hash default) is the fact that the former evaluates default only if it is necessary. (or (:key hash) default)(:key hash default)之间的关键区别在于前者仅在必要时才评估default In the latter case it is always evaluated. 在后一种情况下,它总是被评估。 Therefore you should use or if an evaluation of default is expensive. 因此,您应该使用or如果default评估是昂贵的。

Another difference becomes apparent when your hash contains values which are false in a boolean context. 当您的哈希包含布尔上下文中的false值时,另一个区别变得明显。 In cases of such values (or (:key hash) default) will be evaluated to default instead of false or nil which you expect. 如果这些值(or (:key hash) default)将被评估为default而不是您期望的falsenil In contrast to the or expression, (:key hash default) will yield correct results. or表达式相反, (:key hash default)将产生正确的结果。 As a side note, think twice before storing nil as values in a hash. 作为旁注,在将nil作为值存储在哈希中之前要三思而后行。

Fine, those were the important differences. 好吧,那些是重要的区别。 Now let's move to minor ones. 现在让我们转向次要的。

(or (:title opts) "Default title")

is expanded by the reader to 由读者扩展到

;; Redacted for the sake of brevity.
(let* [x (:title opts)]
  (if x
    x
    "Default title"))

Arguably, it is less efficient than to simply evaluate 可以说,它比简单评估效率低

(:title opts "Default title")

Of course without any benchmarks it is hard to estimate the difference is speed, however I believe that it should be marginal. 当然没有任何基准,很难估计差异是速度,但我认为它应该是微不足道的。

On the other hand, at first glance (or (:key hash) :default) seems to be easier to understand for someone not used to the (:key hash :default) idiom. 另一方面,对于不熟悉(:key hash :default)习惯用法的人来说,乍一看(or (:key hash) :default)似乎更容易理解。 Consider programmers coming from other languages. 考虑来自其他语言的程序员。 In Ruby for instance the typical approach to handling a non existant element of a hash is 例如,在Ruby中,处理散列的非存在元素的典型方法是

val = hash[:key] || :default

Hence, the first expression might be easier to parse by humans not accustomed to certain Clojure's idioms. 因此,第一个表达可能更容易被不习惯某些Clojure成语的人解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM