Mercurial > lasercutter
view src/clojure/contrib/ns_utils.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and2 ;; distribution terms for this software are covered by the Eclipse Public3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can4 ;; be found in the file epl-v10.html at the root of this distribution. By5 ;; using this software in any fashion, you are agreeing to be bound by the6 ;; terms of this license. You must not remove this notice, or any other,7 ;; from this software.9 ;; scgilardi (gmail)10 ;; 23 April 200812 ;; DEPRECATED in 1.2: dir and print-dir. Use dir and dir-fn in13 ;; clojure.repl.15 (ns16 ^{:author "Stephen C. Gilardi",17 :doc "Namespace utilities19 get-ns returns the namespace named by a symbol or throws20 if the namespace does not exist22 ns-vars returns a sorted seq of symbols naming public vars23 in a namespace25 print-docs prints documentation for the public vars in a26 namespace28 immigrate Create a public var in this namespace for each29 public var in the namespaces named by ns-names.30 From James Reeves32 vars returns a sorted seq of symbols naming public vars33 in a namespace (macro)35 docs prints documentation for the public vars in a36 namespace (macro)"}37 clojure.contrib.ns-utils38 (:use clojure.contrib.except))40 ;; Namespace Utilities42 (defn get-ns43 "Returns the namespace named by ns-sym or throws if the44 namespace does not exist"45 [ns-sym]46 (let [ns (find-ns ns-sym)]47 (throw-if (not ns) "Unable to find namespace: %s" ns-sym)48 ns))50 (defn ns-vars51 "Returns a sorted seq of symbols naming public vars in52 a namespace"53 [ns]54 (sort (map first (ns-publics ns))))56 (defn print-dir57 "Prints a sorted directory of public vars in a namespace"58 {:deprecated "1.2"}59 [ns]60 (doseq [item (ns-vars ns)]61 (println item)))63 (defn print-docs64 "Prints documentation for the public vars in a namespace"65 [ns]66 (doseq [item (ns-vars ns)]67 (print-doc (ns-resolve ns item))))69 ;; Convenience71 (defmacro vars72 "Returns a sorted seq of symbols naming public vars in73 a namespace"74 [nsname]75 `(ns-vars (get-ns '~nsname)))77 (defmacro dir78 "Prints a sorted directory of public vars in a namespace"79 {:deprecated "1.2"}80 [nsname]81 `(print-dir (get-ns '~nsname)))83 (defmacro docs84 "Prints documentation for the public vars in a namespace"85 [nsname]86 `(print-docs (get-ns '~nsname)))88 (defn immigrate89 "Create a public var in this namespace for each public var in the90 namespaces named by ns-names. The created vars have the same name, root91 binding, and metadata as the original except that their :ns metadata92 value is this namespace."93 [& ns-names]94 (doseq [ns ns-names]95 (require ns)96 (doseq [[sym var] (ns-publics ns)]97 (let [sym (with-meta sym (assoc (meta var) :ns *ns*))]98 (if (.hasRoot var)99 (intern *ns* sym (.getRoot var))100 (intern *ns* sym))))))