Skip to content

Instantly share code, notes, and snippets.

@grekko
Created April 24, 2015 14:12
Show Gist options
  • Save grekko/8c1b2482f5c20ed888cf to your computer and use it in GitHub Desktop.
Save grekko/8c1b2482f5c20ed888cf to your computer and use it in GitHub Desktop.
One-Dimensional Game of Life in one sane line of ES6 JS
describe('Game of life', function() {
it('should evolve exist', function() {
assert.equal(typeof e, 'function');
});
it('should evolve return same length list', function() {
var board = [0, 0, 0, 0, 0];
assert.equal(e(board).length, board.length);
});
it('should evolve the list', function() {
var board = [1, 1, 1]; // expected: [0, 1, 0]
assert.deepEqual(e(board), [0, 1, 0]);
});
it('should evolve the list', function() {
var board = [1, 1, 1, 1]; // expected: [0, 1,1, 0]
assert.deepEqual(e(board), [0, 1, 1, 0]);
});
it('should evolve the list', function() {
var board = [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1];
// expected: [0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0]
assert.deepEqual(e(board), [0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0]);
});
});
//var e = (b) => b.map((_, i, b) => b[i-1] && b[i+1] ? 1 : 0 );
//var e = (b) => b.map((_, i, b) => b[i-1] && b[i+1] );
var e = (b) => b.map((_, i, b) => +(!!(b[i-1] && b[i+1])));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment