You could write it like this:

(defn scenario-improve []
  (let [ns (str "aigympoc." aigympoc.config/scenario)]
    (eval (read-string (str "(do (require '" ns ") (" ns "/improve)")))))

So we require, then run the function. However, we don't need to use eval
here if we require the namespace and find the var directly:

(defn scenario-improve []
  (let [ns (symbol (str "aigympoc." aigympoc.config/scenario))]
    (require ns)
    (let [improve (find-var (symbol ns "improve"))]
      (improve))))

On Sun, 29 Jul 2018 at 18:50, 'Daniel de Oliveira' via Clojure <
clojure@googlegroups.com> wrote:

> Hi there,
>
> I wanted to use dynamic loading of namespaces to achieve sort of a plugin
> structure but got kind of struck.
>
> I have a leiningen project, with the following files:
>
> -- src/aigympoc/config.clj
>
> (ns aigympoc.config)
>
> (def scenario "aigympoc.scenario1")
>
>
> - src/aigympoc/scenario1.clj
>
> (ns aigympoc.scenario1)
>
> (defn improve [] ...)
> (defn success? [] ...)
>
>
> - src/aigympoc/scenario2.clj
>
> (ns aigympoc.scenario2)
>
> (defn improve [] ...)
> (defn success? [] ...)
>
>
> - src/aigympoc/gameloop.clj
>
>
> (ns aigympoc.gameloop
>   (:require
>     aigympoc.config
>     ...
>     aigympoc.scenario1    ; <-- how to get
>     aigympoc.scenario2))) ; <-- rid of these
>
> ...
>
>
> (defn step
>
>   []
>
>    ...
>       (do
>         (if (= (scenario-success?) \t)
>           (prn "success")
>           (do (scenario-improve)
>
>
> (defn scenario-improve
>   []
>   (eval (read-string (str "(aigympoc." aigympoc.config/scenario 
> "/improve)"))))
>
>
> (defn scenario-success?
>   []
>   (eval (read-string (str "(aigympoc." aigympoc.config/scenario 
> "/success?)"))))
>
>
> I would really like to get rid of the requires to aigympoc.scenario1 and 
> aigympoc.scenario1,
>
> so that scenarios can get loaded on the fly, by just putting a new file to a 
> folder and setting the name in the config (or giving it on runtime).
>
> I spent quite some time on it, but cannot figure it out.
>
>
> I'm really new to clojure so I think I may be trying to use it
> non-idiomatically and there are better ways for doing it.
> In any case I would be happy if someone more experienced could point me in
> the right direction.
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


-- 
James Reeves
booleanknot.com

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to