As Jonas says, don't use `for` for side effects.

The mass of code is because it's lazily consuming the sequence, so there's
extra code to handle the "as needed" production and processing of values.

On Sat, 15 Nov 2014 11:04 Jonas Enlund <[email protected]> wrote:

> for returns a lazy sequence and since you don't use the result the body is
> never evaluated. You should use doseq instead:
>
>     (doseq [x (range 1 10)]
>        (.log js/console x))
>
>
> http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/for
> http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/doseq
>
> On Saturday, November 15, 2014 12:48:31 PM UTC+2, Aleksandr wrote:
> > Hi
> >
> > Experimenting with clojurescript, confused about loop structures.
> >
> > Having a simple expression:
> > (for [x (range 1 10)]
> >   (.log js/console x))
> >
> > I get nothing logged.
> > My optimization is set to :whitespace.
> > The construction is compiled into rather large blob of code:
> >
> > var iter__4379__auto___5647 = function iter__5643(s__5644) {
> >   return new cljs.core.LazySeq(null, function() {
> >     var s__5644__$1 = s__5644;
> >     while (true) {
> >       var temp__4126__auto__ = cljs.core.seq.call(null, s__5644__$1);
> >       if (temp__4126__auto__) {
> >         var s__5644__$2 = temp__4126__auto__;
> >         if (cljs.core.chunked_seq_QMARK_.call(null, s__5644__$2)) {
> >           var c__4377__auto__ = cljs.core.chunk_first.call(null,
> s__5644__$2);
> >           var size__4378__auto__ = cljs.core.count.call(null,
> c__4377__auto__);
> >           var b__5646 = cljs.core.chunk_buffer.call(null,
> size__4378__auto__);
> >           if (function() {
> >             var i__5645 = 0;
> >             while (true) {
> >               if (i__5645 < size__4378__auto__) {
> >                 var x = cljs.core._nth.call(null, c__4377__auto__,
> i__5645);
> >                 cljs.core.chunk_append.call(null, b__5646,
> alert("test"));
> >                 var G__5648 = i__5645 + 1;
> >                 i__5645 = G__5648;
> >                 continue;
> >               } else {
> >                 return true;
> >               }
> >               break;
> >             }
> >           }()) {
> >             return cljs.core.chunk_cons.call(null,
> cljs.core.chunk.call(null, b__5646), iter__5643.call(null,
> cljs.core.chunk_rest.call(null, s__5644__$2)));
> >           } else {
> >             return cljs.core.chunk_cons.call(null,
> cljs.core.chunk.call(null, b__5646), null);
> >           }
> >         } else {
> >           var x = cljs.core.first.call(null, s__5644__$2);
> >           return cljs.core.cons.call(null, alert("test"),
> iter__5643.call(null, cljs.core.rest.call(null, s__5644__$2)));
> >         }
> >       } else {
> >         return null;
> >       }
> >       break;
> >     }
> >   }, null, null);
> > };
> >
> >
> >
> > Using loop / recur instead
> >
> > (loop [x 0]
> >   (if (< x 10)
> >     (do (.log js/console x)
> >         (recur (+ x 1)))))
> >
> > Works as expected and compiles to:
> > while (true) {
> >   if (x_5667 < 10) {
> >     towers.core.log.call(null, x_5667);
> >     var G__5668 = x_5667 + 1;
> >     x_5667 = G__5668;
> >     continue;
> >   } else {
> >   }
> >   break;
> > }
> >
> >
> > Am I missing out something? It for / range loop supposed to work?
> >
> > Thanks!
>
> --
> Note that posts from new members are moderated - please be patient with
> your first post.
> ---
> You received this message because you are subscribed to the Google Groups
> "ClojureScript" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/clojurescript.
>

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to