首页 > 解决方案 > clojure 的“doc”命令打印出来是什么?

问题描述

我对clojure的有点困惑doc

输出是参数的规范,使用传统的基本正则表达式标记,即:

但是,圆括号()和方括号[]“按给定”用于形成正确的表达式形式,而不用于对任何正则表达式元素进行分组。

如果有几个有效的参数组合,它们将被列在几行上。

但例如观察输出(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

前三行是有意义的:

  1. var特殊形式的包和名称
  2. 有效的表达式结构(fn name? [params*] exprs*)
  3. 另一种可能的表达结构(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])再次,我不清楚的含义。

标签: clojure

解决方案


这些是函数的参数列表。

来源

来自“fn”宏定义的代码

([& 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]),这就是它在源代码中定义的方式

来自“letfn”宏定义的代码

fnspecs是第一个参数,之后提供的任何参数都被分组到bodyvar-arg 列表中。您可能需要查看Clojure 的 var-arg syntax


推荐阅读