On Friday, September 23, 2016 at 11:11:07 AM UTC-5, Alan Thompson wrote:
>
> ​Huh.  I was also unaware of the run! function.​
>
> I suppose you could always write it like this:
>
> (def x (vec (range 3)))
> (def y (vec (reverse x)))
>
> (run!
>   (fn [[x y]] (println x y))
>
>   (map vector x y))
>
>
>  > lein run
> 0 2
> 1 1
> 2 0
>
>
Yes.  But that's got the same problem.  Doesn't matter with a toy example, 
but the (map vector ...) could be undesirable with large collections in 
performance-critical code.

although the plain old for loop with dotimes looks simpler:
>
> (dotimes [i (count x) ]
>   (println (x i) (y i)))
>
>
> maybe that is the best answer? It is hard to beat the flexibility of a a 
> loop and an explicit index.
>

I agree that this is clearer, but it kind of bothers me to index through a 
vector sequentially in Clojure.  We need indexing In Clojure because 
sometimes you need to access a vector more arbitrarily.  If you're just 
walking the vector in order, we have better methods--as long as we don't 
want to walk multiple vectors in the same order for side effects.

However, the real drawback of the dotimes method is that it's not efficient 
for the general case; it could be slow on lists, lazy sequences, etc. 
(again, on non-toy examples).  Many of the most convenient Clojure 
functions return lazy sequences.  Even the non-lazy sequences returned by 
transducers aren't efficiently indexable, afaik.  Of course you can always 
throw any sequence into 'vec' and get out a vector, but that's an 
unnecessary transformation if you just want to iterate through the 
sequences element by element.

If I'm writing a function that will plot points or that will write data to 
a file, it shouldn't be a requirement for the sake of efficiency that the 
data come in the form of vectors.  I should be able to pass in the data in 
whatever form is easiest.  Right now, if I wanted efficiency for walking 
through sequences in the same order, without creating unnecessary data 
structures, I'd have to write the function using loop/recur.  On the other 
hand, if I wanted the cross product of the sequences, I'd use doseq and be 
done a lot quicker with clearer code.

-- 
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.

Reply via email to