clojure - clojure 的“doc”命令打印出来是什么?
问题描述
我对clojure的有点困惑doc
。
输出是参数的规范,使用传统的基本正则表达式标记,即:
*
: 0..n 标记的前面元素的实例(“许多”)?
: 0..1 标记的前面元素的实例(“可选”)+
: 1..n 标记的前面元素的实例(“至少一个”)
但是,圆括号()
和方括号[]
“按给定”用于形成正确的表达式或形式,而不用于对任何正则表达式元素进行分组。
如果有几个有效的参数组合,它们将被列在几行上。
但例如观察输出(doc fn)
:
clojure.core/fn
(fn name? [params*] exprs*)
(fn name? ([params*] exprs*) +)
([& sigs])
Special Form
params => positional-params* , or positional-params* & next-param
positional-param => binding-form
next-param => binding-form
name => symbol
Defines a function
Please see http://clojure.org/special_forms#fn
params => positional-params* , or positional-params* & next-param
positional-param => binding-form
next-param => binding-form
name => symbol
Defines a function
Spec
args: (cat :fn-name (? simple-symbol?) :fn-tail (alt :arity-1 :clojure.core.specs.alpha/params+body :arity-n (+ (spec :clojure.core.specs.alpha/params+body))))
ret: any?
nil
前三行是有意义的:
- var或特殊形式的包和名称
- 有效的表达式结构
(fn name? [params*] exprs*)
- 另一种可能的表达结构
(fn name? ([params*] exprs*) +)
但是还有一个非缩进的([& sigs])
. 那是怎么回事?
随后的输出似乎也需要清理。什么是“规格”?
对于(doc doc)
:
clojure.repl/doc
([name])
Macro
Prints documentation for a var or special form given its name,
or for a spec if given a keyword
nil
我不明白非缩进的([name])
.
或者让我们试试(doc letfn)
:
clojure.core/letfn
(letfn [fnspecs*] exprs*)
([fnspecs & body])
Special Form
fnspec ==> (fname [params*] exprs) or (fname ([params*] exprs)+)
Takes a vector of function specs and a body, and generates a set of
bindings of functions to their names. All of the names are available
in all of the definitions of the functions, as well as the body.
fnspec ==> (fname [params*] exprs) or (fname ([params*] exprs)+)
Takes a vector of function specs and a body, and generates a set of
bindings of functions to their names. All of the names are available
in all of the definitions of the functions, as well as the body.
nil
([fnspecs & body])
再次,我不清楚的含义。
解决方案
这些是函数的参数列表。
从来源:
([& sigs])
说它需要一个“(sig)natures”的可变参数列表。在这种情况下,“签名”指的是前面几行的示例表单。要么([params] body)
用于创建具有单个参数列表的函数,要么用于创建(([params] body) ([other-params] body)
具有多个参数列表的函数的列表。
一个doc
只是说doc
接受 aname
作为参数。
最后,letfn
是说它需要一个 " fnspecs
" 向量和一个可变参数body
表达式。您可以在它的使用方式中看到这一点:
(letfn [(some-function [param] ; The vector on these two lines is "fnspecs"
(println param))]
(println "PRINTING!") ; These two lines are grouped into "body"
(some-function 1))
它有一组外部括号,因为它显示了所有可用参数列表的列表。doc
显示([name])
(一个向量的列表),因为它只有一个有效的参数列表。如果您查看文档中的某个功能,例如max
:
clojure.core/max
([x] [x y] [x y & more])
Returns the greatest of the nums.
它列出了多个参数列表,因为它max
有多个参数。
至于为什么letfn
显示([fnspec & body])
,这就是它在源代码中定义的方式:
fnspecs
是第一个参数,之后提供的任何参数都被分组到body
var-arg 列表中。您可能需要查看Clojure 的 var-arg syntax。
推荐阅读
- android - Android DataBinding:即使生成也无法访问 LayoutNameDataBinding
- sql - 使用当前计数和时间戳列添加先前的加载计数
- javascript - JS for循环中是否有redo语句?
- azure - 使用数据工厂将表从 SAP BW 提取到 Azure Data Lake Gen2
- python - 根据拆分后的模式拆分文件的最简单方法是什么?
- python - 使用 uwsgi 时出现 conda env 问题
- scikit-learn - 隐藏 RandomizedSearchCV 输出
- qt - 是否可以更改可检查 QGroupBox 的默认行为?
- c++ - C++ 指针(数组)
- python - Python 中 %y 和 %Y 之间不同 CSV 表的日期时间不匹配