From 67ca92283cf2ac8263f1d9a2657215a47315851e Mon Sep 17 00:00:00 2001 From: Glenn Vanderburg Date: Mon, 2 Jan 2023 11:31:57 -0600 Subject: [PATCH 1/2] use reductions to create the lazy seq in binary-tree --- src/main/snergly/algorithms.cljc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/snergly/algorithms.cljc b/src/main/snergly/algorithms.cljc index cb303b8..2bcc569 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 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 binary-tree-seq [grid] - (binary-tree-seq* (g/begin-step (assoc grid ::g/algorithm-name "binary-tree")) (g/grid-positions grid))) + (reductions binary-tree-seq* (assoc grid ::g/algorithm-name "binary-tree") (g/grid-positions grid))) (defn sidewinder-end-run? [cell] (let [on-east-side? (not (g/cell-neighbor cell :east)) From 6e4ca9f2bb66d59fccc0e20f969a29fca1bbf444 Mon Sep 17 00:00:00 2001 From: Glenn Vanderburg Date: Mon, 2 Jan 2023 11:56:07 -0600 Subject: [PATCH 2/2] unify binary-tree-seq and binary-tree-seq* Try out the approach of bundling the public and helper functions into one function with multiple arities, rather than two functions. --- src/main/snergly/algorithms.cljc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/snergly/algorithms.cljc b/src/main/snergly/algorithms.cljc index 2bcc569..06cf8ee 100644 --- a/src/main/snergly/algorithms.cljc +++ b/src/main/snergly/algorithms.cljc @@ -57,16 +57,16 @@ ;; have the algorithm name as a name prefix (for example, ;; sidewinder-step). -(defn binary-tree-seq* [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 binary-tree-seq [grid] - (reductions binary-tree-seq* (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))