Skip to content

Commit

Permalink
green: refactor and extract uniquePusherFor()
Browse files Browse the repository at this point in the history
  • Loading branch information
dobbs committed Feb 28, 2011
1 parent 98f960c commit b866f98
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lifespec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ describe("Conway's Game of Life", function () {
return neighbors;
}
function each(arr, fn) {var i = arr.length; while (i--) {fn.apply(arr[i]);}}
function fertileCellsFrom(generation) {
function uniquePusherFor(arr) {
var seen = {};
return function (cell) {if (! seen[cell.join()]++) {arr.push(cell);}}
}
function fertileCellsFrom(generation) {
var fertileCells = [];
function pushUnique(arr, cell) {if (! seen[cell.join()]) {fertileCells.push(cell);}}
var pushUnique = uniquePusherFor(fertileCells);
each(generation, function () {
pushUnique(fertileCells, this);
each(neighbors(this), function () {
pushUnique(fertileCells, this)
});
pushUnique(this);
each(neighbors(this), function () {pushUnique(this)});
});
return fertileCells;
}
Expand Down

0 comments on commit b866f98

Please sign in to comment.