mount

fabrao 2016-05-31T19:04:24.000692Z

Hello all, Why this don´t work?

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

tolitius 2016-05-31T19:18:12.000693Z

@fabrao: for is lazy

tolitius 2016-05-31T19:18:54.000694Z

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)

tolitius 2016-05-31T19:19:08.000695Z

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

tolitius 2016-05-31T19:19:31.000696Z

i.e.

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

fabrao 2016-05-31T19:20:11.000697Z

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

tolitius 2016-05-31T19:20:44.000698Z

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

tolitius 2016-05-31T19:21:04.000699Z

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

tolitius 2016-05-31T19:23:06.000700Z

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)

fabrao 2016-05-31T19:23:39.000701Z

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

fabrao 2016-05-31T19:24:12.000702Z

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

tolitius 2016-05-31T19:24:46.000703Z

what would you like shaper to be once started?

fabrao 2016-05-31T19:26:49.000704Z

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

fabrao 2016-05-31T19:27:48.000705Z

(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)))

tolitius 2016-05-31T19:28:38.000706Z

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

fabrao 2016-05-31T19:29:30.000707Z

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

fabrao 2016-05-31T19:30:37.000708Z

I´ll try with doseq

tolitius 2016-05-31T19:30:40.000709Z

ok, so you want this list of futures?

fabrao 2016-05-31T19:31:23.000710Z

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

tolitius 2016-05-31T19:34:25.000711Z

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)

fabrao 2016-05-31T19:36:23.000714Z

HUMMMMMM, I´ll try it

tolitius 2016-05-31T19:36:30.000715Z

doall realizes lazy seqs

tolitius 2016-05-31T19:36:50.000716Z

i.e. http://stackoverflow.com/a/1642840/211277

fabrao 2016-05-31T19:38:22.000718Z

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

tolitius 2016-05-31T19:42:08.000719Z

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.

fabrao 2016-05-31T19:44:00.000720Z

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

tolitius 2016-05-31T19:44:16.000721Z

sure, glad it works for you 🙂

fabrao 2016-05-31T19:53:03.000722Z

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

tolitius 2016-05-31T19:56:08.000723Z

right, a start function should return

tolitius 2016-05-31T19:57:14.000724Z

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