clojure - doseq should be giving me side effects so why does it work with dorun only? -


i wrote simple program takes number n , list input. , prints each element in list n times.

i tried this

(defn list-repl [num lst]   (doseq [elem lst]     (map println (repeat num elem)))) 

this didn't work. no output. while looking @ docs found dorun. tried , did work.

(defn list-repl [num lst]   (doseq [elem lst]     (dorun (map println (repeat num elem))))) 

reading documentation understand doseq looping construct forces side-effects in body-expression. dorun directly sequences.

is understanding correct? if correct body in first example should have given me side effect of printing number. did not happen. missing in understanding?

basically, doseq can't force every side effect in body. in order guarantee have recursively check every expression in body lazy subcomputations force, be... challenging. deals top-level expressions. forcing lazy subcomputations job of expressions' writer.

if need several layers of looping "unpack" members of outer sequence, remember doseq, for , other variants support nested loops providing more 1 binding:

(defn list-repl [num lst]   (doseq [elem lst           to-print (repeat num elem)]     (println to-print))) 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -