clojure 中如何使用“*var-name*”命名约定?
作为一个接触 clojure 的非 lisper,我应该如何最好地理解 vars 得到像 *var-name*
这样的名称的命名约定?
这似乎是指示全局变量的 lisp 约定。但据我所知,在 clojure 中,此类变量出现在名称空间中。
我真的很感激当作者在他们的代码中使用这样的变量时我应该期待什么的简短解释,最好有一个例子来说明如何以及为什么在 clojure 库中使用和更改这样的变量。
As a non-lisper coming to clojure how should I best understand the naming convention where vars get a name like *var-name*
?
This appears to be a lisp convention indicating a global variable. But in clojure such vars appear in namespaces as far as I can tell.
I would really appreciate a brief explanation of what I should expect when an author has used such vars in their code, ideally with a example of how and why such a var would be used and changed in a clojure library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是其他 Lisp(例如 Common Lisp)中使用的约定,用于区分 < em>特殊变量,与词汇变量不同。 特殊或动态变量的绑定存储在动态环境中,这意味着它的当前值对代码中的任何点可见取决于它如何被绑定到更高的位置向上调用堆栈,而不是仅依赖于最本地的词法绑定形式(例如
let
或defn
)。请注意,Doug Hoyte 在他的著作 Let Over Lambda 中反对“耳罩” asterix 约定用于命名特殊变量。他使用了一种不寻常的宏风格来引用自由变量,并且他不喜欢承诺或区分这些符号最终是否会引用词汇变量或动态变量。
虽然专门针对 Common Lisp,但您可能会喜欢 Ron Garret 的文章 特殊变量白痴指南。其中大部分内容仍然适用于 Clojure。
It's a convention used in other Lisps, such as Common Lisp, to distinguish between special variables, as distinct from lexical variables. A special or dynamic variable has its binding stored in a dynamic environment, meaning that its current value as visible to any point in the code depends upon how it may have been bound higher up the call stack, as opposed to being dependent only on the most local lexical binding form (such as
let
ordefn
).Note that in his book Let Over Lambda, Doug Hoyte argues against the "earmuffs" asterix convention for naming special variables. He uses an unusual macro style that makes reference to free variables, and he prefers not to commit to or distinguish whether those symbols will eventually refer to lexical or dynamic variables.
Though targeted specifically at Common Lisp, you might enjoy Ron Garret's essay The Idiot's Guide to Special Variables. Much of it can still apply to Clojure.
函数式编程就是关于安全的可预测函数。事实上,我们中的一些人害怕那种令人毛骨悚然的“远距离作用”的事情。当人们调用一个函数时,他们会得到一种温暖而模糊的满足感,即如果他们再次调用该函数或读取该值,该函数总是会给出相同的结果。
*un-warm-and-fuzzy*
毛茸茸的东西的存在是为了警告程序员,这个变量比其他一些变量不太可爱。Functional programming is all about safe predictable functions. Infact some of us are afraid of that spooky "action at a distance" thing. When people call a function they get a warm fuzzy satisfaction that the function will always give them the same result if they call the function or read the value again. the
*un-warm-and-fuzzy*
bristly things exist to warn programmers that this variable is less cuddly than some of the others.我在 Clojure 新闻组中找到的一些参考资料:
和...
Some references I found in the Clojure newsgroups:
and...