Clojure 什么是fn*,它与fn有何不同?
我查看了Clojure 什么是fn*,它与fn有何不同?,clojure,Clojure,我查看了lazy seq的源代码,发现: Clojure 1.4.0 user=> (source lazy-seq) (defmacro lazy-seq "Takes a body of expressions that returns an ISeq or nil, and yields a Seqable object that will invoke the body only the first time seq is called, and will cache t
lazy seq
的源代码,发现:
Clojure 1.4.0
user=> (source lazy-seq)
(defmacro lazy-seq
"Takes a body of expressions that returns an ISeq or nil, and yields
a Seqable object that will invoke the body only the first time seq
is called, and will cache the result and return it on all subsequent
seq calls. See also - realized?"
{:added "1.0"}
[& body]
(list 'new 'clojure.lang.LazySeq (list* '^{:once true} fn* [] body)))
nil
user=>
我想知道
fn*
与fn
有何不同,但我似乎在文档中找不到任何关于fn*
的参考。我遗漏了什么,以及fn*
有何不同?免责声明:我离Clojure编译器专家还有很长的路要走,所以请在以下内容中加入适量的盐
fn*
是fn
的固有版本。Clojure的大部分是用Clojure实现的,但一些低级函数是用Java实现的fn*
就是这样一个例子
fn
是根据fn*
实现的。资料来源如下:
fn*
在编译器中实现:
实际上有两种不同版本的fn*
,在Java中用符号fn
和FNONCE
来表示。第一个是“裸”的fn*
,第二个是fn*
,元数据为^{:once-true}
,这是lazy-seq使用的版本
^{:once-true}
用于让编译器知道包含函数的闭包将只被调用一次,并且它可以执行closed-over-local清除。有关详细信息,请参阅。这是正确的。我要补充的是,fn*
是一个实现细节,而不是clojure API的一部分;这就是为什么它没有包含在文档中。