简体   繁体   中英

Get the source of clojure.zip

Obviously, I can go to the Clojure's Github page or look in my maven repository, this is not a question about literally getting the source code.

I want to programmatically get all the top level forms defined in a namespace. Something like

(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_....]

Basically, just getting a ordered sequence of all the forms in a namespace, in the order they were defined. Is this possible?

This will extract top-level forms from the copy of the clojure.zip source bundled inside the 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))) ...)

That should do the job:

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

Which returns:

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

Other ns-* functions can be found in the namespaces page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM