Add fp version
This commit is contained in:
parent
4186f80a09
commit
3062fe3171
@ -21,3 +21,10 @@ with the arrow keys.
|
|||||||
## Broken
|
## Broken
|
||||||
|
|
||||||
This one is not working. Not bothering to fix it, got no git history.
|
This one is not working. Not bothering to fix it, got no git history.
|
||||||
|
|
||||||
|
|
||||||
|
## Functional
|
||||||
|
|
||||||
|
Functional implementation.
|
||||||
|
|
||||||
|

|
||||||
|
|||||||
36
fp/ca-fp.html
Executable file
36
fp/ca-fp.html
Executable file
@ -0,0 +1,36 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>CA</title>
|
||||||
|
|
||||||
|
<style type="text/css">
|
||||||
|
#viewport {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
|
||||||
|
margin: auto;
|
||||||
|
|
||||||
|
padding: 10px;
|
||||||
|
height: 640px;
|
||||||
|
width: 900px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#board {
|
||||||
|
border: 1px solid;
|
||||||
|
border-color: '#bbb';
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="viewport">
|
||||||
|
<canvas id="board" height="600" width="800"></canvas>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="fplib.js"></script>
|
||||||
|
<script src="ca-fp.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
124
fp/ca-fp.js
Executable file
124
fp/ca-fp.js
Executable file
@ -0,0 +1,124 @@
|
|||||||
|
|
||||||
|
var getEle = fp.curry(function(ele) {
|
||||||
|
return (document.querySelectorAll(ele).length === 1) ? document.querySelector(ele) : [...document.querySelectorAll(ele)];
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
function hexColor() {
|
||||||
|
var values = "01233456789ABCDEF";
|
||||||
|
var result = [];
|
||||||
|
for(var i = 0; i < 6; i++) {
|
||||||
|
result.push(values[Math.floor(Math.random() * 16)]);
|
||||||
|
}
|
||||||
|
return '#'+result.join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
var css = fp.curry(function(cssObj, ele) {
|
||||||
|
for(var key in cssObj) {
|
||||||
|
ele.style[key] = cssObj[key];
|
||||||
|
}
|
||||||
|
return ele;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//--------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
var count_neighbours = function(r, c, grid) {
|
||||||
|
var count = 0;
|
||||||
|
if (r > 0) {
|
||||||
|
count += grid[r-1][c];
|
||||||
|
}
|
||||||
|
if (r < grid.length-1) {
|
||||||
|
count += grid[r+1][c];
|
||||||
|
}
|
||||||
|
if (c > 0) {
|
||||||
|
count += grid[r][c-1];
|
||||||
|
}
|
||||||
|
if (c < grid.length[0]-1) {
|
||||||
|
count += grid[r][c+1];
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var update_grid = function(grid) {
|
||||||
|
var count;
|
||||||
|
var nrows = grid.length;
|
||||||
|
var ncols = grid[0].length;
|
||||||
|
for (var r=0; r<nrows; r++) {
|
||||||
|
for (var c=0; c<ncols; c++) {
|
||||||
|
count = count_neighbours(r, c, grid);
|
||||||
|
if (grid[r][c] == 1 && count > 2) {
|
||||||
|
grid[r][c] = 0;
|
||||||
|
}
|
||||||
|
if (grid[r][c] == 0 && count <= 2) {
|
||||||
|
grid[r][c] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return grid;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var render_grid = fp.curry(function(ctx, grid) {
|
||||||
|
var height = ctx.canvas.height;
|
||||||
|
var width= ctx.canvas.width;
|
||||||
|
var nrows = grid.length;
|
||||||
|
var ncols = grid[0].length;
|
||||||
|
|
||||||
|
ctx.clearRect(0, 0, width, height);
|
||||||
|
for (var r=0; r<nrows; r++) {
|
||||||
|
for (var c=0; c<ncols; c++) {
|
||||||
|
ctx.fillStyle = (grid[r][c] == 0) ? '#fff':'#000';
|
||||||
|
ctx.fillRect(c*width/ncols+0.5, r*height/nrows+0.5, width/ncols-0.5, height/nrows-0.5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return grid;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
var app = function(canvas_id, timestep, grid_init) {
|
||||||
|
var canvas = document.getElementById(canvas_id);
|
||||||
|
var ctx = canvas.getContext('2d');
|
||||||
|
var render = render_grid(ctx);
|
||||||
|
var state = grid_init;
|
||||||
|
setInterval(function() {
|
||||||
|
|
||||||
|
//state = render_grid(ctx, update_grid(state));
|
||||||
|
state = fp.pipe(render, update_grid)(state);
|
||||||
|
}, timestep);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var grid0 = [
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,1,1,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0],
|
||||||
|
[1,0,0,0,1,1,1,1,0,0,0,0]
|
||||||
|
];
|
||||||
|
|
||||||
|
var random_grid = function(nrows, ncols) {
|
||||||
|
var grid = []
|
||||||
|
for (var r=0; r<nrows; r++) {
|
||||||
|
grid[r] = [];
|
||||||
|
for (var c=0; c<ncols; c++) {
|
||||||
|
grid[r][c] = (Math.random() > 0.8) ? 1:0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log(grid);
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
//app("board", 500, grid0);
|
||||||
|
app("board", 500, random_grid(200, 200));
|
||||||
27
fp/fplib.js
Executable file
27
fp/fplib.js
Executable file
@ -0,0 +1,27 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
var fp = {
|
||||||
|
curry,
|
||||||
|
compose,
|
||||||
|
pipe
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function curry(fn) {
|
||||||
|
return function(...args) {
|
||||||
|
if(fn.length > args.length) {
|
||||||
|
var arg = args;
|
||||||
|
return function(...args) {return fn.apply(null, arg.concat(args));};
|
||||||
|
}
|
||||||
|
return fn.apply(null, args);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function compose(f,g) {
|
||||||
|
return function(...x) {return f(g(...x));}
|
||||||
|
}
|
||||||
|
|
||||||
|
function pipe(...fns) {
|
||||||
|
return fns.reduce(compose);
|
||||||
|
}
|
||||||
|
|
||||||
BIN
fp/recording.gif
Normal file
BIN
fp/recording.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 187 KiB |
Loading…
x
Reference in New Issue
Block a user