Clojure namespace declaration by functions experiment
(ns nsfn | |
(:require | |
[clojure.string :as s] | |
[clojure.java.io :as jio])) | |
(declare require*) | |
(defn ns* | |
" | |
Define a namespace with name a-ns | |
(or aka a-ns if given in params) | |
with the given params | |
" | |
[params a-ns] | |
(let [cns *ns* | |
path-cljc (str (s/replace (name a-ns) #"\." "/") ".cljc") | |
ns-fn (eval (read-string (slurp (jio/resource path-cljc)))) | |
{:keys [namespace-name defs requires]} (ns-fn params) | |
the-ns (or (get-in params [:aka a-ns]) namespace-name) | |
aliases (map last (filter (fn [r] (filter :as r)) requires)) | |
] | |
(in-ns the-ns) | |
(refer-clojure) | |
(when (not-empty aliases) | |
(apply ns-unalias the-ns aliases)) | |
(when (not-empty requires) | |
(apply require* params requires)) | |
(doseq [[sym body] defs] | |
(eval `(def ~sym ~body))) | |
(in-ns (ns-name cns)))) | |
(defn require* [params [a-ns as aliaz]] | |
(ns* params a-ns) | |
(alias aliaz (or (get-in params [:aka a-ns]) a-ns))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Hendekagon commentedNov 9, 2019
•
edited
This is a quick & rough experiment with creating namespaces
using functions to create them which take parameters so that
one can make alternative implementations of namespaces
I am not suggesting this a good way to achieve this functionality!
It's just a sketch of what I'm thinking about today
(require :reload '[nsfn :as n])
Require namespace
c
asbc
, creating the namespaceb.c
-- an implementation of
c
wherec
's aliasd
is aliased to namespaceb
(n/require* {:requires '{d b} :aka '{c b.c}} '[c :as bc])
Require namespace
c
asac
, creating the namespacea.c
-- an implementation of
c
wherec
's aliasd
is aliased to namespacea
(n/require* {:requires '{d a} :aka '{c a.c}} '[c :as ac])
Namespaces
a
andb
both provide the same API, 1 functionx
which
c
uses via its aliasd
. We can switch the namespace aliased tod
with the parameters sent to the function which creates the namespace
c
One can imagine other parameters being useful in the creation of a namespace