Skip to content

Instantly share code, notes, and snippets.

View marick's full-sized avatar

Brian Marick marick

View GitHub Profile
(defn successor [cell]
(condp = (living-neighbor-count cell)
3 (vivified cell)
2 (if (living? cell)
(vivified cell)
(killed cell))
(killed cell))
)
(pending border unborder
living? living-neighbor-count killed vivified)
(defn world []
(println "executing root binding")
"root value")
(defn f [ignored-value]
(println "within f, world at" world "produces" (world))
(world)
)
(println "Root world function is at" world)
(know "how living neighbors are counted"
(living-neighbor-count ...center...) => 1
(provided
(neighbors ...center...) => [ ...living-cell... ...dead-cell... ]
(living? ...living-cell...) => true
(living? ...dead-cell...) => false)
)
(defn living-neighbor-count [cell]
(count (filter living? (neighbors cell)))
)
(know "that neighbors are constructed from coordinates alone"
(neighbors (cell-at 3 88) => (have-coordinates
[2 89] [3 89] [4 89]
[2 88] [4 88]
[2 87] [3 87] [4 87])
)
user=> (def product (cartesian-product [-1 0 1] [-1 0 1]))
#'user/product
user=> product
((-1 -1) (-1 0) (-1 1) (0 -1) (0 0) (0 1) (1 -1) (1 0) (1 1))
user=> (def meaningful-values (remove #{ [0 0] } product))
#'user/meaningful-values
user=> meaningful-values
((-1 -1) (-1 0) (-1 1) (0 -1) (0 1) (1 -1) (1 0) (1 1))
user=> (#{1} 1)
1
user=> (#{1} 0)
nil
user=> (def cell [3 88])
#'user/cell
user=> (defn shifter [[x-shift y-shift]] [ (+ (first cell) x-shift)
(+ (second cell) y-shift)])
#'user/shifter
user=> (shifter [-1 1])
[2 89]
user=> (map shifter meaningful-values)
([2 87] [2 88] [2 89] [3 87] [3 89] [4 87] [4 88] [4 89])