diff --git a/src/main/snergly/algorithms.cljc b/src/main/snergly/algorithms.cljc index cb303b8..06cf8ee 100644 --- a/src/main/snergly/algorithms.cljc +++ b/src/main/snergly/algorithms.cljc @@ -57,18 +57,16 @@ ;; have the algorithm name as a name prefix (for example, ;; sidewinder-step). -(defn binary-tree-seq* [grid [coord & coords]] - (lazy-seq - (when coord - (let [cell (g/grid-cell grid coord) - neighbors (g/cell-neighbors cell [:north :east]) - next-grid (if (empty? neighbors) - grid - (g/link-cells grid cell (rand-nth neighbors)))] - (cons next-grid (binary-tree-seq* (g/begin-step next-grid) coords)))))) - -(defn binary-tree-seq [grid] - (binary-tree-seq* (g/begin-step (assoc grid ::g/algorithm-name "binary-tree")) (g/grid-positions grid))) +(defn binary-tree-seq + ([grid] + (reductions binary-tree-seq (assoc grid ::g/algorithm-name "binary-tree") (g/grid-positions grid))) + ([grid coord] + (let [grid (g/begin-step grid) + cell (g/grid-cell grid coord) + neighbors (g/cell-neighbors cell [:north :east])] + (if (empty? neighbors) + grid + (g/link-cells grid cell (rand-nth neighbors)))))) (defn sidewinder-end-run? [cell] (let [on-east-side? (not (g/cell-neighbor cell :east))