Last active
March 25, 2018 08:23
-
-
Save octopuss/aeffb926fffd3d94ecfc7a3025aa5ef5 to your computer and use it in GitHub Desktop.
GOL ramda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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