JSFiddle - React, Tailwind, and code Playground
by evgkch
JavaScript
const EDGE = 5;
let STORE = new Map();
const BUFFER_STORE = new Map();
const createArray = (length) => Array.from( {length: length}, (v, k) => [k, true] )
const shuffle = (array) => array.sort( (a, b) => Math.random() - 0.5 )
const randomStore = (array) => (edge) => (n) =>
(n < edge * edge)
? array.slice(0, n)
: false
STORE = new Map ( randomStore(shuffle(createArray(8)))(EDGE)(8) )
const getNextCellState = (handleFn) => (store) => ([ key , state ]) =>
( handleFn(store)(key) === 2 )
? [key, state]
: ( handleFn(store)(key) === 3 )
? [key, true]
: [key, false]
const handleCellNeighborhoods = (countFn) => (returnFn) => (middlewareFn) => (store) => (key) => {
if ( typeof middlewareFn === 'function') middlewareFn(store)(returnFn(key))
return countFn(store)(returnFn(key));
}
const countFromArea = (conditionsFn) => (store) => ([ first , ...rest ]) =>
([...rest].length > 0)
? conditionsFn(store)(first) + countFromArea(conditionsFn)(store)([...rest])
: conditionsFn(store)(first)
const hasCellNeighborhoods = (store) => (key) => ( store.has(key) ) ? 1 : 0
const getCellArea = (fn) => (metricFn) => ([ x , y ]) =>
new Array(
[x - 1 , y - 1], [x , y - 1], [x + 1 , y - 1],
[x - 1 , y ], [x + 1 , y ],
[x - 1 , y + 1], [x , y + 1], [x + 1 , y + 1]
).map( ([ x , y ]) => fn([ metricFn(x) , metricFn(y) ]) )
const applyToricMetric = (edge) => (pos) => (pos === -1) ? edge - 1 : (pos === edge) ? 0 : pos
const xy2Key = (edge) => ([ x , y ]) => x + edge * y
const keyToXY = (edge) => (key) => [ Math.floor(key % edge) , Math.floor(key / edge) ]
const setEntryToStore = (toStore) => (fromStore) => {
return function iterator(keys) {
for (let key of keys) {
if ( !fromStore.has(key) && !toStore.has(key) ) toStore.set(key, false)
}
}
}
const _xy2Key = xy2Key(EDGE)
const _keyToXY = keyToXY(EDGE)
const _applyToricMetric = applyToricMetric(EDGE)
const _getCellArea = (key) =>...