Created
June 10, 2010 21:23
-
-
Save marick/433652 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; Based on an idea by Paul Blair and Michael Nicholaides @nicholaides | |
(def *horizontal-blinker* [ [0,1] [1,1], [2,1] ] ) | |
(def *vertical-blinker* [ [1,2] | |
[1,1] | |
[1,0] ]) | |
(example "of blinkers blinking" | |
(next-world *horizontal-blinker*) => *vertical-blinker* | |
(next-world *vertical-blinker*) => *horizontal-blinker* | |
) | |
(defn next-world [world] | |
(-> world | |
border tick unborder) | |
) | |
(defn tick [bordered-world] | |
(against-background [world bordered-world] | |
(map successor bordered-world) | |
) | |
) | |
(example "of rules for life or death of a cell in the successor generation" | |
(successor ...cell...) => dead | |
(whenever ; underpopulation | |
... cell... => alive | |
(living-neighbor-count ...cell...) =within=> [0, 1]) | |
(or ; overpopulation | |
... cell... => alive | |
(living-neighbor-count ...cell...) =within=> [3 ,,, 8]) | |
(or ; not enough cells to bring to life | |
... cell... => dead | |
(living-neighbor-count ...cell...) =within=> [0,,,2]) ; | |
(or ; too many cells to bring to life | |
... cell... => dead | |
(living-neighbor-count ...cell...) =within=> [4,,,8]) ; | |
(successor ...cell...) => alive | |
(whenever | |
...cell... => alive | |
(living-neighbor-count ...cell...) =within=> [2, 3]) | |
(or | |
...cell... => dead | |
(living-neighbor-count ...cell...) => 3) | |
) | |
(example "of knowing things about the neighbors of a cell" | |
(living-neighbor-count ...cell...) => 1 | |
(whenever | |
(neighbors ...cell...) => [...dead-cell... ...alive-cell...] | |
...dead-cell... => dead | |
...alive-cell... => alive) | |
(neighbors (cell-at 3 88)) => (has-coordinates | |
[2 89] [3 89] [4 89] | |
[2 88] [4 88] | |
[2 87] [3 87] [4 87]) | |
) | |
(example "the bordered world is a map from cell to its life-value" | |
(living? ...cell...) => truthy | |
(whenever | |
(world) => { ...cell... :alive }) | |
(living? ...cell...) => falsey | |
(whenever | |
(world) => { ...cell... :dead }) | |
(example "cells are just arrays" | |
(make-cell 1 2) => [1 2] | |
(x (make-cell [1 2])) => 1 | |
(y (make-cell 1 2)) => 2 | |
) | |
(example "of reducing the results to their original form" | |
(unborder [ (killed (make-cell 1 1)) | |
(vivified (make-cell 2 2)) ]) => [ (make-cell 2 2) ] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All the more proof to me hay Clojure is not a language meant for mere mortal. I wish stupider people would show me some idiomatic Clojure. Perhaps then I'd see the light.