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))))) |
avysk
commented
May 3, 2011
Yeah.... That's better. I prefer that alive? returns a boolean though.
…On May 3, 2011, at 4:12 PM, avysk wrote:
```
(defn neighbors-of [cell]
[for [dx [-1 0 1] dy [-1 0 1] :when (not (= [dx dy] [0 0]))]
(map + [dx dy] cell)])
(defn alive? [world [cell freqs]]
(when (or (and (= 2 freqs) (contains? world cell)) (= 3 freqs)) cell))
(defn tick [world]
(let [fr (frequencies (mapcat neighbors-of world))]
(set (keep (partial alive? world) fr))))
```
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/950574
Well, you can move when
-- keep #(when (alive? world %) %)
Or rename alive?
to something like grow-cell
:-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment