Created
May 1, 2011 15:22
-
-
Save slagyr/950574 to your computer and use it in GitHub Desktop.
Game of Life in 8 lines of Clojure
This file contains 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
(defn neighbors-of [cell] | |
(set (for [dx [-1 0 1] dy [-1 0 1] :when (not (= [dx dy] [0 0]))] | |
[(+ dx (first cell)) (+ dy (second cell))]))) | |
(defn alive? [[cell freqs] world] | |
(or (and (= 2 freqs) (contains? world cell)) (= 3 freqs))) | |
(defn tick [world] | |
(let [frequencies (frequencies (reduce #(concat %1 (neighbors-of %2)) [] world))] | |
(set (keys (filter #(alive? % world) frequencies))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, you can move
when
--keep #(when (alive? world %) %)
Or rename
alive?
to something likegrow-cell
:-)