Skip to content

Instantly share code, notes, and snippets.

@octopuss
Last active March 25, 2018 08:23
Show Gist options
  • Save octopuss/aeffb926fffd3d94ecfc7a3025aa5ef5 to your computer and use it in GitHub Desktop.
Save octopuss/aeffb926fffd3d94ecfc7a3025aa5ef5 to your computer and use it in GitHub Desktop.
GOL ramda
const board = [[0,0], [1,1], [0,1]];
const neigh = ([x,y]) => [[x-1, y], [x, y-1], [x+1, y+1], [x, y+1], [x+1, y],[x-1, y-1], [x-1, y+1], [x+1, y-1]];
let mapcat = fn => transduce(map(fn), concat,[]);
const filterIndexed = addIndex(filter);
const cellFromKey = (key) => compose(map(Number), split(','))(key);
const isAlive = (i, counts, board) => contains(cellFromKey(keys(counts)[i]), board);
const golRules = (counts, board) => (x, i) => x === 3 || (x===2 && isAlive(i, counts, board));
const filterSurroundings = (board) => (counts) => filterIndexed(golRules(counts, board))(counts);
compose(
map(cellFromKey),
keys,
filterSurroundings(board),
countBy(identity),
mapcat(neigh)
)(board);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment