hara.namespace utilities for namespace manipulation

Author: Chris Zheng  (z@caudate.me)
Date: 29 June 2017
Repository: https://github.com/zcaudate/hara
Version: 2.5.10

hara.namespace contain namespace functions and utilities

1    namespace.eval

Add to project.clj dependencies:

[im.chit/hara.namespace.eval "2.5.10"]

hara.namespace.eval allows for evaluation within a given namespace



eval-ns ^

evaluates a list of forms in an existing namespace

v 2.2
(defn eval-ns
  [ns forms]
  (binding [*ns* (the-ns ns)]
    (->> forms
         (mapv (fn [form] (eval form)))
         last)))
link
(eval-ns 'hara.common.checks '[(long? 1)]) => true

eval-temp-ns ^

evaluates a list of forms in a temporary namespace

v 2.2
(defn eval-temp-ns
  [forms]
  (let [sym (gensym)]
    (try
      (create-ns sym)
      (eval-ns sym (cons '(clojure.core/refer-clojure) forms))
      (finally (remove-ns sym)))))
link
(eval-temp-ns '[(def inc1 inc) (defn inc2 [x] (+ 1 x)) (-> 1 inc1 inc2)]) => 3 "All created vars will be destroyed after evaluation." (resolve 'inc1) => nil

with-ns ^

evaluates `body` forms in an existing namespace given by `ns`.

v 2.0
(defmacro with-ns
  [ns & forms]
  `(binding [*ns* (the-ns ~ns)]
     ~@(map (fn [form] `(eval '~form)) forms)))
link
(require '[hara.common.checks]) (with-ns 'hara.common.checks (long? 1)) => true

with-temp-ns ^

evaluates `body` forms in a temporary namespace.

v 2.0
(defmacro with-temp-ns
  [& forms]
  `(try
     (create-ns 'sym#)
     (let [res# (with-ns 'sym#
                  (clojure.core/refer-clojure)
                  ~@forms)]
       res#)
     (finally (remove-ns 'sym#))))
link
(with-temp-ns (def inc1 inc) (defn inc2 [x] (+ 1 x)) (-> 1 inc1 inc2)) => 3 "All created vars will be destroyed after evaluation." (resolve 'inc1) => nil

2    namespace.import

Add to project.clj dependencies:

[im.chit/hara.namespace.eval "2.5.10"]

hara.namespace.import allows for moving function definitions around



import ^

imports all or a selection of vars from one namespace to the current one.

v 2.0
(defmacro import
  [nsp vars & more]
  `(do
     (require (quote ~nsp))
     (import-namespace
      (quote ~nsp)
      ~(if-not (= :all vars)
         `(quote ~vars)))
     ~(if more
        `(import ~@more))))
link
(import hara.common.checks [bytes? long?]) => nil (eval '(long? 1)) => true (eval '(bytes? 1)) => false (import hara.common.checks :all) => nil (eval '(bigint? 1)) => false

import-namespace ^

imports all or a selection of vars from one namespace to the current one.

v 2.0
(defn import-namespace
  ([ns] (import-namespace ns nil))
  ([ns vars]
     (let [all-vars (ns-publics ns)
           selected-vars (if vars
                            (select-keys all-vars vars)
                            all-vars)]
       (doseq [[n v] selected-vars]
         (import-var n v)))))
link
(import-namespace 'hara.common.checks '[bytes? long?]) (eval '(long? 1)) => true (eval '(bytes? 1)) => false

import-var ^

imports a single var from one namespace to the current one.

v 2.0
(defn import-var
  [name ^clojure.lang.Var var]
  (if (.hasRoot var)
    (intern *ns* (with-meta name (merge (meta var)
                                        (meta name)))
            @var)))
link
(import-var 'ifl #'clojure.core/if-let) => anything ; #'hara.namespace.import-test/ifl (eval '(ifl [a 1] (inc a))) => 2

3    namespace.resolve

hara.namespace.resolve allows for resolving namespaces



ns-vars ^

lists the vars in a particular namespace

v 2.1
(defn ns-vars
  [ns]
  (vec (sort (keys (ns-publics ns)))))
link
(ns-vars 'hara.namespace.resolve) => '[ns-vars resolve-ns]

resolve-ns ^

resolves the namespace or else returns nil if it does not exist

v 2.1
(defn resolve-ns
  [^clojure.lang.Symbol sym]
  (let [nsp  (.getNamespace sym)
        nsym (or  (and nsp
                       (symbol nsp))
                  sym)]
    (if nsym
      (suppress (do (require nsym) nsym)))))
link
(resolve-ns 'clojure.core) => 'clojure.core (resolve-ns 'clojure.core/some) => 'clojure.core (resolve-ns 'clojure.hello) => nil