5 ms·
Game of life implemented as sets in Clojure: (defn neighbors [[x y]] #{[(dec x) (dec y)] [(dec x) y] [(dec x) (inc y)] [ x (dec y)] [
by twp 2y ago
Game of life implemented as sets in Clojure:
(defn neighbors [[x y]]
#{[(dec x) (dec y)] [(dec x) y] [(dec x) (inc y)]
[ x (dec y)] [ x (inc y)]
[(inc x) (dec y)] [(inc x) y] [(inc x) (inc y)]})
(defn count-neighbors [world cell]
(count (set/intersection (neighbors cell) world)))
(def rules #{[true 2] [true 3] [false 3]})
(defn live [world cell]
(contains? rules [(contains? world cell) (count-neighbors world cell)]))
(defn evolve [world]
(into #{} (filter #(live world %) (reduce set/union (map neighbors world)))))
Full source: https://github.com/twpayne/life/blob/master/clojure/src/life/core.clj https://github.com/twpayne/life/blob/master/clojure/src/life...
- drpossum 2y agoWhy is this the best one? Or is it just an implementation you like?