Created
January 6, 2014 01:37
-
-
Save dlongmuir/8276813 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
;; cell | |
[1 2] | |
;; world | |
#{ [1 0] [1 1] [1 2]} | |
;; compute neighbors of a cell | |
(defn neighbors [[x y]] | |
(for [dx [-1 0 1] | |
dy (if (zero? dx) | |
[-1 1] | |
[-1 0 1])] | |
[(+ x dx) (+ y dy)] ) ) | |
(neighbors [1 1]) | |
(def blinker #{[1 0] [1 1] [1 2]}) | |
(map neighbors blinker) | |
(mapcat neighbors blinker) | |
(frequencies (mapcat neighbors blinker)) | |
(defn live [n alive?] | |
(or (= n 3) | |
(and (= n 2) alive?)) ) | |
(live 2 false) | |
(live 3 false) | |
(live 2 true) | |
;; putting it all together | |
(class blinker) | |
;; the blinker cell stuff | |
(#{[1 0]} [1 0]) | |
(defn step [world] | |
(set | |
(for [[cell n] (frequencies(mapcat neighbors world)) | |
:when (live n (world cell))] | |
cell)) ) | |
(step (step blinker)) | |
(defn life [initial-world] | |
(iterate step initial-world)) | |
(take 5 (life blinker)) | |
(def glider #{ [0 1] [1 2] [2 0] [2 1] [2 2]}) | |
(take 5 (life glider)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment