I'm working on a small game in Clojure as a learning exercise. I think I've settled on a representation of the game state at any particular time as a list of "movables" and a 2D vector-of-vectors for the "terrain" (board squares).
95% of the time I expect to be checking for a collision in a particular square for which the 2D vector seems appropriate. But in a few cases, I need to go the other direction -- find the (x,y) location of a cell that matches some criteria. First attempt was something like this:
(defn find-cell-row [fn row x y]
(if (empty? row) nil
(if (fn (first row)) [x y]
(find-cell-row fn (rest row) (inc x) y))))
(defn find-cell [fn grid y]
(if (empty? grid) nil
(or (find-cell-row fn (first grid) 0 y)
(find-cell (rest grid) (inc y)))))
(def sample [[\a \b \c][\d \e \f]])
(find-cell #(= % \c) sample 0) ;; => [2 0]
I tried something more concise with map-indexed, but it got ugly quickly and still didn't give me quite what I wanted. Is there a more idiomatic way to do this search, or perhaps I would be better served with a different data structure? Maybe a map { [x y] -> cell }? Using a map to represent a matrix feels so wrong to me :)