Re: Using dynamically loaded namespaces like plug-ins

2018-07-29 Thread 'Daniel de Oliveira' via Clojure
Hi James,

thank you so much, that really gave me the right direction. 
I integrated your proposal and refactored until I got 

- config.clj

(ns aigympoc.config)

(def scenario "aigympoc.scenario2")


- gameloop.clj

(ns aigympoc.gameloop
  (:gen-class
:init init)

(defn -init
  []
  (require (symbol aigympoc.config/scenario)))
(defn scenario-success?
  []
  ((find-var (symbol aigympoc.config/scenario "success?"
(defn scenario-improve
  []
  ((find-var (symbol aigympoc.config/scenario "improve"


This now does what I wanted. I can configure and load the ns at runtime and 
use this as sort of an plug in interface.
Thank you very much!

PS:
What was important for me in the PoC was that it would work without aot'ing 
the scenario2.clj, which it does.










Am Sonntag, 29. Juli 2018 20:38:36 UTC+2 schrieb James Reeves:
>
> 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 <
> clo...@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 clo...@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+u...@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+u...@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.


Using dynamically loaded namespaces like plug-ins

2018-07-29 Thread 'Daniel de Oliveira' via Clojure
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.