Fork me on GitHub
#mount
<
2016-05-31
>
fabrao19:05:24

Hello all, Why this don´t work?

(defn inicia []
  (for [id (range 10)]
    (println "ID => " id)))
(defstate shaper
  :start (inicia) )

tolitius19:05:54

i.e.

dev=> (defn inicia [] (for [id (range 10)] (println "ID => " id)))
#'dev/inicia
dev=> (defstate shaper :start (inicia))
">> starting.. #'dev/shaper (namespace was recompiled)"
#'dev/shaper
dev=> shaper
ID =>  0
ID =>  1
ID =>  2
ID =>  3
ID =>  4
ID =>  5
ID =>  6
ID =>  7
ID =>  8
ID =>  9
(nil nil nil nil nil nil nil nil nil nil)

tolitius19:05:08

if you'd like to see results on start, use doseq

tolitius19:05:31

i.e.

(defn inicia [] (doseq [id (range 10)] (println "ID => " id)))

fabrao19:05:11

@tolitius: well, I´d like to set the results to shaper, as for returns

tolitius19:05:44

you'd like shaper to initialize to (nil nil nil nil nil nil nil nil nil nil)?

tolitius19:05:04

because that is what (for [id (range 10)] (println "ID => " id)) returns

tolitius19:05:06

in case you just want a range:

dev=> (defstate foo :start (range 10))
#'dev/foo
dev=> (mount/start #'dev/foo)
{:started ["#'dev/foo"]}
dev=> foo
(0 1 2 3 4 5 6 7 8 9)

fabrao19:05:39

I tried with overtone.at-at that creates a schedule object, but I had to call shaper in repl to processes starting up

fabrao19:05:12

that´s because I tried with println, to see if the prints out

tolitius19:05:46

what would you like shaper to be once started?

fabrao19:05:49

I read the config from file that define devices to monitoring, from config file that returns a list of devices, on each I want to start a schedule with overtone.at-at

fabrao19:05:48

(for [device (:device config)]
    (let
      [nome-device (name (key device))
       conf-device ((key device) (:device config))
       cadencia (Integer. (:cadencia (:shaper conf-device)))]
      (log/debug (str "Iniciando processo Traffic Shaper para " nome-device))
      (at/every (* cadencia 1000) #(processo-shaper nome-device conf-device storage) pool :desc nome-device :fixed-delay true)))

tolitius19:05:38

right, so for is lazy, if you'd like them to be scheduled when this expression is called, use doseq

fabrao19:05:30

ok, so I have to include the instances in a list to return

fabrao19:05:37

I´ll try with doseq

tolitius19:05:40

ok, so you want this list of futures?

fabrao19:05:23

yes, because in :stop the schedules jobs must be stoped

tolitius19:05:25

dev=> (defn inicia []
        (doall (for [id (range 10)]
                 (let [foo 42]
                   (println "ID => " id)
                   (+ id foo)))))
#'dev/inicia
dev=> (defstate fooz :start (inicia))
#'dev/fooz
dev=> (mount/start #'dev/fooz)
ID =>  0
ID =>  1
ID =>  2
ID =>  3
ID =>  4
ID =>  5
ID =>  6
ID =>  7
ID =>  8
ID =>  9
{:started ["#'dev/fooz"]}
dev=> fooz
(42 43 44 45 46 47 48 49 50 51)

fabrao19:05:23

HUMMMMMM, I´ll try it

tolitius19:05:30

doall realizes lazy seqs

fabrao19:05:22

the problem was I didn´t know that lazy seqs don´t work with mount start, thanks a lot 4 ur help

tolitius19:05:08

sure. lazy seqs work with mount/start i.e. a state becomes a lazy seq. but in your case you do not need a lazy seq, since you'd like these calls to every executed as the function is called.

fabrao19:05:00

I changed including doall and it works, thanks for your help

tolitius19:05:16

sure, glad it works for you 🙂

fabrao19:05:03

A question, if a start blocks in a loop, the starting system will freeze up?

tolitius19:05:08

right, a start function should return

tolitius19:05:14

otherwise the state will not/never have the value until the loop is done