mirror of
https://github.com/janet-lang/janet
synced 2024-11-28 11:09:54 +00:00
4e4dd31164
Add quasiquote, unquote, and unquote-splicing as specials rather than a macro.
44 lines
1.0 KiB
Plaintext
44 lines
1.0 KiB
Plaintext
# A game of life implementation
|
|
|
|
(def- window
|
|
(seq [x :range [-1 2]
|
|
y :range [-1 2]
|
|
:when (not (and (zero? x) (zero? y)))]
|
|
(tuple x y)))
|
|
|
|
(defn- neighbors
|
|
[[x y]]
|
|
(map (fn [[x1 y1]] (tuple (+ x x1) (+ y y1))) window))
|
|
|
|
(defn tick
|
|
"Get the next state in the Game Of Life."
|
|
[state]
|
|
(def cell-set (frequencies state))
|
|
(def neighbor-set (frequencies (mapcat neighbors state)))
|
|
(seq [coord :keys neighbor-set
|
|
:let [count neighbor-set@coord]
|
|
:when (or (= count 3) (and (get cell-set coord) (= count 2)))]
|
|
coord))
|
|
|
|
(defn draw
|
|
"Draw cells in the game of life from (x1, y1) to (x2, y2)"
|
|
[state x1 y1 x2 y2]
|
|
(def cellset @{})
|
|
(each cell state (:= cellset@cell true))
|
|
(loop [x :range [x1 (+ 1 x2)]
|
|
:after (print)
|
|
y :range [y1 (+ 1 y2)]]
|
|
(file/write stdout (if (get cellset (tuple x y)) "X " ". ")))
|
|
(print))
|
|
|
|
#
|
|
# Run the example
|
|
#
|
|
|
|
(var *state* '[(0 0) (-1 0) (1 0) (1 1) (0 2)])
|
|
|
|
(for i 0 20
|
|
(print "generation " i)
|
|
(draw *state* -7 -7 7 7)
|
|
(:= *state* (tick *state*)))
|