为什么clojure的向量函数定义是如此冗长? [英] Why clojure's vector function definition is so verbose?

查看:123
本文介绍了为什么clojure的向量函数定义是如此冗长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很好奇为什么vector的实现是如此冗长?它不能只是 [] [a] [a & args]

I'm really curious to see why vector's implementation is so verbose? What's the reason it can't just do [], [a] and [a & args]?

这是我从 clj-1.4.0 获得的。

=> (source vector)
(defn vector
  "Creates a new vector containing the args."
  {:added "1.0"
   :static true}
  ([] [])
  ([a] [a])
  ([a b] [a b])
  ([a b c] [a b c])
  ([a b c d] [a b c d])
  ([a b c d & args]
     (. clojure.lang.LazilyPersistentVector (create (cons a (cons b (cons c (cons d args))))))))
nil


推荐答案

前几种情况有直接调用更快,因为他们是最常见的。在很少的情况下,它被调用与许多参数可能需要更多的电话,因此更多的时间,但这保持常见的情况简洁。 这是一个蓄意的速度,冗长的折衷。它还使得函数的使用从查看参数列表清楚,没有杂乱的人们的IDE与大量的列表。

The first few cases have direct calls to make them faster because they are the most commonly called. The rare cases where it is called with many arguments may require more calls and hence more time, but this keeps the common cases concise. It was a deliberate speed, verbosity tradeoff. It also makes the use of the function clear from looking at the argument list without cluttering people's IDEs with a huge list of arities.

Clojure的核心功能大部分具有相似的签名。

Most of Clojure's core functions have similar signatures.

这篇关于为什么clojure的向量函数定义是如此冗长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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