Trying to write idiomatic clojure to create vector of vectors

928 views
Skip to first unread message

Rising_Phorce

unread,
Jul 22, 2010, 8:37:20 AM7/22/10
to Clojure
I'm coming from an imperative background and trying to stay away from
"for loops" to do the task at hand. Given a size, I want to create a
vector of vectors to represent a game board similar to Rich's ants or
Conway's Game of Life (no wrapping necessary for me). A couple of
questions. I will need to traverse the vectors sequentially but at
each step there will be lots of sequential back and forward tracking.
Arrays would be the natural imperative choice, but the docs recommend
this only for inter-op. So are vectors the best choice?

Secondly, I figured out how to create a row (a vector of length
'size'): (defn make-row [size] (vec (take size (repeat nil))))

but I can't figure out how to conj 'size' calls to make-row onto
another empty vector.

I've tried 'map', but then I need a data structure of length 'size' to
iterate

I've looked at 'doTimes', but haven't been successful in figured out
how to make it work for this.

Thanks.

Edmund Jackson

unread,
Jul 23, 2010, 7:13:59 AM7/23/10
to clo...@googlegroups.com
Hi,

Some time back Lau Jensen blogged something like this. Its starts here

http://www.bestinclass.dk/index.clj/2009/10/brians-functional-brain.html

but there are at least two follow ups. It might spark inspiration if you've not yet read it.

Edmund

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Konrad Hinsen

unread,
Jul 23, 2010, 10:19:41 AM7/23/10
to clo...@googlegroups.com
On 22 Jul 2010, at 14:37, Rising_Phorce wrote:

> I will need to traverse the vectors sequentially but at
> each step there will be lots of sequential back and forward tracking.
> Arrays would be the natural imperative choice, but the docs recommend
> this only for inter-op. So are vectors the best choice?

Probably, but it's hard to say without knowing what exactly you need
to do on this data structure, and how often. Some other options are:
- a map from (x, y) coordinates to board values
- a vector of vectors of maps, with each map containg the board value
and references to neighbouring cells

> Secondly, I figured out how to create a row (a vector of length
> 'size'): (defn make-row [size] (vec (take size (repeat nil))))
>
> but I can't figure out how to conj 'size' calls to make-row onto
> another empty vector.

Instead of (take size (repeat nil)), you can write (replicate size
nil). You can then make a square board filled with nils with

user> (defn square-board [size] (vec (replicate size (vec (replicate
size nil)))))
#'user/square-board
user> (square-board 3)
[[nil nil nil] [nil nil nil] [nil nil nil]]

Konrad.

Meikel Brandmeyer

unread,
Jul 23, 2010, 10:30:39 AM7/23/10
to Clojure
Hi,

On Jul 23, 4:19 pm, Konrad Hinsen <konrad.hin...@fastmail.net> wrote:

> Instead of (take size (repeat nil)), you can write (replicate size  
> nil). You can then make a square board filled with nils with

Or just (repeat size nil). I think replicate was replaced by the two-
arg form of repeat.

Sincerely
Meikel
Reply all
Reply to author
Forward
0 new messages