【问题标题】:What is clojure's "doc" command printing out?clojure 的“doc”命令打印出来是什么?
【发布时间】:2019-10-12 12:32:41
【问题描述】:

我对 clojure 的 doc 有点困惑。

输出是参数的规范,使用传统的basic regular expression markup,即:

  • *: 0..n 标记的前面元素的实例(“many”)
  • ?: 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

前三行的含义如下:

  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


    【解决方案1】:

    这些是函数的参数列表。

    来自the source

    ([& sigs]) 说它需要一个“(sig)natures”的可变参数列表。在这种情况下,“签名”指的是前面几行的示例表单。 ([params] body) 用于创建具有单个参数列表的函数,或者 (([params] body) ([other-params] body) 列表用于创建具有多个参数列表的函数。

    doc 只是说doc 接受name 作为参数。

    最后,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]),就是way that it's defined in the source

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

    【讨论】:

    • 是的,对不起,我第一次做对了。我正在从 Clojure 中休息来学习 C 语言,但显然有些知识已经从我的脑海中消失了。并对这些位添加更多解释。
    • @DavidTonhofer 已更新。让我知道这是否可以解决。如果不是,你问后者的语法呢?
    • 是的,我现在明白了。谢谢!祝 C 语言好运,这一定是 Clojure 之后的逆向文化冲击。
    • @DavidTonhofer Ya,用 10 行来反转一个字符串,然后需要处理分配的内存是一个 PITA。它当然没有表现力或简洁。
    • 重点是不缩进的([& sigs])([name])([fnspecs & body]) 是实际的参数声明,括在括号中doc 例程。微妙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 2023-02-06
    • 1970-01-01
    相关资源
    最近更新 更多