code-reviews

slipset 2016-08-20T13:44:48.000017Z

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

slipset 2016-08-20T13:45:11.000018Z

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

2016-08-20T19:41:07.000020Z

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!

slipset 2016-08-20T20:06:43.000022Z

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

slipset 2016-08-20T20:13:45.000023Z

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

slipset 2016-08-20T20:17:52.000024Z

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

slipset 2016-08-20T20:18:09.000025Z

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

slipset 2016-08-20T20:18:44.000026Z

(get-in-game game 4 3)

slipset 2016-08-20T20:18:53.000027Z

;=> [4 3]

2016-08-20T21:42:49.000028Z

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

2016-08-20T21:50:42.000029Z

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.

2016-08-20T21:50:47.000030Z

(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 []))))

2016-08-20T21:50:51.000031Z

Works like a charm!

2016-08-20T21:51:38.000032Z

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.

2016-08-20T21:53:07.000033Z

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.