获取clojure.zip的来源

时间:2013-10-07 05:17:53

标签: clojure

显然,我可以访问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_....]

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

2 个答案:

答案 0 :(得分:2)

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

(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))) ...)

答案 1 :(得分: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- *函数可在namespaces页面中找到。