programextensions/canvas/demos/life.xtl

Conway's Life in a native window: the board is an X_eTaL array, each generation one array expression, drawn by Rust with every frame. Run: just demo canvas life (Space: a new board; q or close: quit)

source · imports cv: extensions/canvas/lib/Canvas.xtl

ᵘl̲ife : (Num a, Truthy a) => a -> a

function · line 9

Life (as in X_eTaL's demos/life.xtl): every cell plus its neighbors by rotating along both axes and summing; alive next at 3, or 4 if alive

ᵘl̲ife ← { ('+ r̲/₁₂ -1 0 1 o̲-₁₂ ⍵) { (⍺ = 3) + ⍵ × ⍺ = 4 } ⍵ }
Used in: ᵘf̲rame

ᵘs̲eed : Truthy a => Int -> a

function · line 12

a random board, about one cell in four alive

ᵘs̲eed ← { n → (n c̲at n) r̲eshape 1 = r̲oll! (n × n) r̲eshape 4 }
Used in: ᵘf̲rame, last

w : Int

value · line 14
w ← "Life -- X_eTaL" ᶜᵛo̲pen! 512 512

ᵘq̲uit : (Any a, Num b) => a -> b

function · line 17

after quitting: close the window; an empty board ends the loop's work

ᵘq̲uit ← { b →
  c ← ᶜᵛc̲lose! w
  0 0 r̲eshape 0
}
Used in: ᵘf̲rame

ᵘf̲rame : (Num a, Truthy a) => a -> a

function · line 23

one frame: draw the board, read an event, step

ᵘf̲rame ← { b →
  0 = t̲ally b ? b
  e ← ᶜᵛn̲ext! w
  ((e m̲atch "close") + e m̲atch "key q") > 0 ? ᵘq̲uit b
  e m̲atch "key Space" ? ᵘs̲eed 96
  s ← w ᶜᵛs̲how! b
  ᵘl̲ife b
}
Used in: last

last : Int

value · line 33

up to 100,000 frames (about half an hour at 60 a second)

last ← 100000 'ᵘf̲rame p̲ower ᵘs̲eed 96