Fork me on GitHub

Still playing with my maze thing—to implement the first algorithm in the book, I realized that I needed to consider each cell in turn, but during each iteration, I might need to operate on the entire grid. For instance, upon deciding that cell 0,0 should be linked east to cell 1,0, I need to get a grid with both cells changed. I solved it by applying the Feynmann Algorithm, and used traverse:

  (fn [grid cell]
    (let [open-directions (filter (partial move grid cell) [::s ::e])]
      (if (empty? open-directions)
        (link grid cell (rand-nth open-directions)))))
  (sm/traverse [s/ALL s/ALL] g))
This totally works! Just curious if I'm missing a simpler way to do the same thing. (The book is in Ruby, so it has an easier time just navigating around the data structure and changing whatever it wants at any point in the loop.)


...looking at the docs and my code a bit harder, I realize that traverse is just there to have a more efficient implementation than select when used as a reduce argument. And the problem of "make changes to a larger resultset while iterating over individual elements" is exactly what reduce is for. So I guess that kind of answers my question. :duck: