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

查看:15
本文介绍了为什么 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天全站免登陆