Both `for` and `doseq` support the same vector form preceding a body. `for`
returns a lazy sequence and is often appropriate for a purely functional
body. `doseq` is not lazy and returns nil, so it is only appropriate when
you want to run the body for side effects.

Take a look at http://clojure.github.io/clojure/clojure.core-api.html and
play around in the repl to get a clearer idea of how that works. Maybe
start with an expression like this:
(let [nested [[1 2 3] [4 5 6]]]
  (for [row nested
        number row]
    (str number " from row " row)))
If you change `for` to `doseq`, you may also want to change `str` to
`println`.

Alan's example uses `map-indexed` to get numeric indices. Note that `cols`
holds a row from `rows` and is then passed to the second `map-indexed`
call, so his example is like a nested for-loop in a C-like language, except
that it is lazy and returns a sequence of results of calling `display` with
each item from your nested vector along with its coordinates. For this
example to make sense, `display` should return a value that will be used to
put something on screen. If `display` just does the screen-putting, and its
return value is insignificant, then `doseq` would make sense.

Hope this helps.
-hume.
On Apr 19, 2013 4:27 AM, <edw...@kenworthy.info> wrote:

> How does that work: you appear to be iterating over two, unconnected,
> vectors.
>
> And yes that's an example of the second option but doesn't explain if or
> why that's the best approach- which was the question ;)
>
> On Thursday, 18 April 2013 19:48:40 UTC+1, Alan Malloy wrote:
>>
>> (for [[y cols] (map-indexed vector rows)
>>       [x cell] (map-indexed vector cols)]
>>   (display cell y x))
>>
>> ?
>>
>> On Thursday, April 18, 2013 3:14:19 AM UTC-7, edw...@kenworthy.infowrote:
>>>
>>> So, I want a 2 dimensional array.
>>>
>>> I think the best way to implement this is a vector of vectors.
>>>
>>> Now I want to display that array drawing each element relative to its
>>> position in the array.
>>>
>>> Is the best way to use doseq and manually maintain the indices? Or is it
>>> to use nested for-loops manually iterating of the vector-of-vectors?
>>>
>>  --
> --
> 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/groups/opt_out.
>
>
>

-- 
-- 
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/groups/opt_out.


Reply via email to