Skip to content

Instantly share code, notes, and snippets.

@jneira
Forked from slagyr/gist:950574
Created May 1, 2011 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jneira/950743 to your computer and use it in GitHub Desktop.
Save jneira/950743 to your computer and use it in GitHub Desktop.
Game of Life in 8 lines of Clojure
(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