Skip to content

Instantly share code, notes, and snippets.

@joejag
Created May 16, 2014 16:47
Show Gist options
  • Save joejag/990e50679e3ec2d3e67c to your computer and use it in GitHub Desktop.
Save joejag/990e50679e3ec2d3e67c to your computer and use it in GitHub Desktop.
Conways GoL: I'm trying out using the threading macro
(ns joejag.conway)
(def world #{[1 2] [2 2] [3 2]})
(defn neighbours-of
[[x y]]
[ [(dec x) (inc y)] [x (inc y)] [(inc x) (inc y)]
[(dec x) y ] [(inc x) y ]
[(dec x) (dec y)] [x (dec y)] [(inc x) (dec y)]
])
(defn survives?
[neighbour-count alive]
(or (= 3 neighbour-count) (and (= 2 neighbour-count) (true? alive))))
(defn conway
[world]
(let [cell-neighbours (frequencies (apply concat (map neighbours-of world)))]
(->> cell-neighbours
(filter #(survives? (second %1) (contains? world (first %1))) )
(map first)
(set))
))
world
(take 3 (iterate conway world))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment