Created
August 16, 2016 14:48
-
-
Save cgrand/de71eef32ea373303c18dbebdedbcf87 to your computer and use it in GitHub Desktop.
transduced game of life
This file contains hidden or 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 xlife | |
(:require [net.cgrand.xforms :as x])) | |
;; xforms is a library of transducers | |
(defn xstep [cells] | |
(into #{} | |
(comp | |
; a comprenhension to generate neighbours | |
(x/for [[x y] % | |
dx [-1 0 1] | |
dy (if (zero? dx) [-1 1] [-1 0 1])] | |
[(+ dx x) (+ dy y)]) | |
; reimplementing frequencies as a transducer | |
(x/by-key identity x/count) | |
; keeping only alive cells | |
(x/for [[cell n] % | |
:when (or (= n 3) (and (= n 2) (cells cell)))] | |
cell)) | |
cells)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment