# Conway's Game of Life in J

I have the following code which produces successive generations of Conway's Game of Life:

life =: 3 : '+./ (>(($y)$ 1);y) *. > =&(+/ (>,{ ;~(1 0 _1))|. y) each 3 4'


Usage:

RR =: 5 7 $0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 NB. First five gens of RR life^:(i.5) RR NB. in ASCII ' *' {~ life^:(i.5) RR  Explanation of code: >,{ ;~(1 0 _1)  Produces all possible 1 rotations of a matrix y. |. y  Applies 1 rotations to y +/  Adds all 1 rotations leading to neighbor-count for each cell =&  Creates a monadic function which takes a single number and compares it to the neighbor-counts each 3 4  Applies the function to 3 and 4 to find cells with 3 or 4 surrounding cells including self alive. >  Unboxes to apply bitwise and (>(($ y) $1);y)  I want all cells that correspond to 3 (($ y) $1) and only the cells that are already currently living the correspond to 4 (y). +./  Or the result together to produce the next generation. I would like to know if there's a better way to do this and shorten code. A lot of the things I did seem very hacky. • Welcome to Code Review! Thanks for including the explanations with your code. Commented Apr 4, 2016 at 21:18 • Shorter code isn't necessarily better code. If that really is your main concern, you may be more interested in Code Golf - but read their How To Ask, and use the Sandbox rather than posting blind! Commented Dec 21, 2022 at 10:28 ## 2 Answers I have written APL code on VM/CMS and in DEC's VAX-11 interpreter. No, I don't see a better way that would shorten the code. I'm a J newbie myself, but how about: life =: 3 : '+./ (>(($ y) \$ 1);y) *. 3 4 =/ +/ (>,{ ;~(1 0 _1))|. y'


This avoids the box-unbox step caused by using each, and uses fewer chars.