Skip to content

Instantly share code, notes, and snippets.

@kolman
Created January 31, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kolman/1516c5f38ab6ab9828e8 to your computer and use it in GitHub Desktop.
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/
(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