Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created August 16, 2016 14:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/de71eef32ea373303c18dbebdedbcf87 to your computer and use it in GitHub Desktop.
Save cgrand/de71eef32ea373303c18dbebdedbcf87 to your computer and use it in GitHub Desktop.
transduced game of life
(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