Fork me on GitHub
#code-reviews
<
2016-08-20
>
slipset13:08:48

not exactly what you want, but if you can live with lists instead of vectors:

slipset13:08:11

(partition (for [x (range 10) y (range 10)] [x y]) 10)

amacdougall19:08:07

Vectors are better for my use case, because I will frequently be looking things up by index (to get the grid cell at [12 59] or whatever). But that's definitely compact!

slipset20:08:43

(->> (for [x (range 10) y (range 10)] [x y])
       (partition 10)
       (mapv (partial into [])))

slipset20:08:45

I guess you could solve this by having one long vector containing (* x y) elements and do indexing by multiplication?

slipset20:08:52

(def game (into [] (for [x (range 10) y (range 10)] [x y])))

slipset20:08:09

(defn get-in-game [game x y] (get game (+ (* 10 x) y)))

slipset20:08:44

(get-in-game game 4 3)

hiredman21:08:49

or if you use a map with keys that are a vector of [x y] you can have a sparse matrix, or just say a matrix is a function that takes two arguments and returns a value, and internal to that function you can do all kinds of stuff

amacdougall21:08:42

Well, the final (?) wrinkle is that each cell also needs to be aware of its links with others. I've generalized this as having a set of valid exits, such as #{::n ::nw ::e}. I'm taking @slipset's excellent suggestion to use a multi-param for and partition it—I had totally forgotten that you don't need to syntactically nest for forms.

amacdougall21:08:47

(defn grid
  "Returns a grid of unconnected cells with the supplied number of columns
  (i.e. width) and rows (i.e. height)."
  [width height]
  (->> (for [y (range 0 height) x (range 0 width)] (create-cell x y))
    (partition width)
    (mapv (partial into []))))

amacdougall21:08:51

Works like a charm!

amacdougall21:08:38

And the exit-set works as long as all the cells are on a uniform grid, which they are, at least for now: if you go east from 0, 0 you'll definitely get to 1, 0.

amacdougall21:08:07

I expect to perform the linkage by passing in an entire grid, wrangling its data a bit with Specter, and getting back an entire grid with that one modification.