Created
January 31, 2015 13:56
-
-
Save kolman/1516c5f38ab6ab9828e8 to your computer and use it in GitHub Desktop.
Game of Life in Clojure, slightly modified from original here: http://clj-me.cgrand.net/2011/08/19/conways-game-of-life/
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
(ns joyful-game.game-of-life) | |
(defn neighs [[x y]] | |
(for [dx [-1 0 1] | |
dy [-1 0 1] | |
:when (not (= 0 dy dx))] | |
[(+ x dx) (+ y dy)])) | |
(defn should-be-alive [nc is-alive] | |
(or (= nc 3) | |
(and (= nc 2) is-alive))) | |
(defn step [board] | |
(->> board | |
(mapcat neighs) | |
frequencies | |
(filter (fn [[cell frq]] (should-be-alive frq (board cell)))) | |
(map first) | |
set)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment