0

显然,我可以去 Clojure 的 Github 页面或查看我的 maven 存储库,这不是字面上获取源代码的问题。

我想以编程方式获取命名空间中定义的所有顶级表单。就像是

(get-top-level-forms-of 'clojure.zip)

=>

['(ns ^{:doc "Functional hierarchical zipper, with navigation, editing,
  and enumeration.  See Huet"
       :author "Rich Hickey"}
  clojure.zip
  (:refer-clojure :exclude (replace remove next)))

 '(defn zipper
  "Creates a new zipper structure. 

  branch? is a fn that, given a node, returns true if can have
  children, even if it currently doesn't.

  children is a fn that, given a branch node, returns a seq of its
  children.

  make-node is a fn that, given an existing node and a seq of
  children, returns a new branch node with the supplied children.
  root is the root node."
  {:added "1.0"}
  [branch? children make-node root]
    ^{:zip/branch? branch? :zip/children children :zip/make-node make-node}
    [root nil])

 '(defn seq-zip
  "Returns a zipper for nested sequences, given a root sequence"
  {:added "1.0"}
  [root]
    (zipper seq?
            identity 
 .....__ALL_THE_REST_OF_THE_FORMS_IN_clojure.zip_....]

基本上,只是按照定义的顺序获取命名空间中所有表单的有序序列。这可能吗?

4

2 回答 2

2

clojure.zip这将从捆绑在 Clojure jar中的源代码副本中提取顶级表单:

(require '[clojure.java.io :as io])

(let [rdr (clojure.lang.LineNumberingPushbackReader.
           (io/reader (io/resource "clojure/zip.clj")))
      sentinel (Object.)]
  (take-while #(not (identical? sentinel %))
              (repeatedly #(read rdr false sentinel))))
;= ((ns clojure.zip (:refer-clojure :exclude (replace remove next))) ...)
于 2013-10-07T07:21:31.523 回答
0

那应该做的工作:

(keys (ns-publics 'clojure.zip))

返回:

(lefts down insert-left up next path children vector-zip append-child zipper branch? end? leftmost edit replace insert-right root insert-child prev seq-zip xml-zip make-node rights node right left remove rightmost)

其他 ns-* 函数可以在命名空间页面中找到。

于 2013-10-07T05:57:16.460 回答