clojure 中如何使用“*var-name*”命名约定?

发布于 2024-08-16 10:47:35 字数 233 浏览 2 评论 0原文

作为一个接触 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚然 2024-08-23 10:47:35

这是其他 Lisp(例如 Common Lisp)中使用的约定,用于区分 < em>特殊变量,与词汇变量不同。 特殊动态变量的绑定存储在动态环境中,这意味着它的当前值对代码中的任何点可见取决于它如何被绑定到更高的位置向上调用堆栈,而不是仅依赖于最本地的词法绑定形式(例如 letdefn)。

请注意,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 or defn).

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.

执手闯天涯 2024-08-23 10:47:35

函数式编程就是关于安全的可预测函数。事实上,我们中的一些人害怕那种令人毛骨悚然的“远距离作用”的事情。当人们调用一个函数时,他们会得到一种温暖而模糊的满足感,即如果他们再次调用该函数或读取该值,该函数总是会给出相同的结果。 *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.

庆幸我还是我 2024-08-23 10:47:35

我在 Clojure 新闻组中找到的一些参考资料:

回复:使代码可读
约翰·休谟
2008 年 12 月 30 日星期二 08:30:57 -0800

2008 年 12 月 29 日星期一下午 4:10,Chouser 写道:
我相信像这样的全球价值观的惯用语是放置星号
围绕名称。

我认为星号约定适用于用于以下目的的变量
动态绑定。我花了一分钟才弄清楚我从哪里得到的
主意。 “Programming Clojure”(没有完全说出来)在
第 6 章第 3 节。

<块引用>

“用于动态绑定的变量有时称为特殊变量-
能够。给它们起名字是很好的风格
带有前导和尾随星号。”

显然这本书正在制作中,但这听起来确实
合理的。对于值发生变化的变量(或
我的代码欢迎重新绑定)对我来说似乎比
“全局变量”(虽然我不确定我会考虑像网格大小这样的东西
对于给定的应用程序是全局的)。基于 ants.clj 看起来丰富
不觉得需要有一个特殊的命名约定
某种价值。

和...

我相信像这样的全球价值观的惯用语是放置星号
名字周围。下划线(和驼峰命名法)仅应在以下情况下使用:
Java 互操作所需:

(def *grid-size* 10)
(def *height* 600)
(def *margin* 50)
(def *x-index* 0)
(def *y-index* 1)

Some references I found in the Clojure newsgroups:

Re: making code readable
John D. Hume
Tue, 30 Dec 2008 08:30:57 -0800

On Mon, Dec 29, 2008 at 4:10 PM, Chouser wrote:
I believe the idiom for global values like this is to place asterisks
around the name.

I thought the asterisk convention was for variables intended for
dynamic binding. It took me a minute to figure out where I got that
idea. "Programming Clojure" suggests it (without quite saying it) in
chapter 6, section 3.

"Vars intended for dynamic binding are sometimes called special vari-
ables. It is good style to name them
with leading and trailing asterisks."

Obviously the book's a work in progress, but that does sound
reasonable. A special convention for variables whose values change (or
that my code's welcome to rebind) seems more useful to me than one for
"globals" (though I'm not sure I'd consider something like grid-size
for a given application a global). Based on ants.clj it appears Rich
doesn't feel there needs to be a special naming convention for that
sort of value.

and...

I believe the idiom for global values like this is to place asterisks
around the name. Underscores (and CamelCase) should only be used when
required for Java interop:

(def *grid-size* 10)
(def *height* 600)
(def *margin* 50)
(def *x-index* 0)
(def *y-index* 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文