Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reductions to create lazy sequences #2

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/main/snergly/algorithms.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down