-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule.js
More file actions
25 lines (22 loc) · 820 Bytes
/
rule.js
File metadata and controls
25 lines (22 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
export const rollout = config => seed => ruleNum => {
let result = [seed]
const genFun = generation (config) (step (config) (buildLUT (config) (ruleNum)))
do result.push (genFun (result [result.length -1]))
while (result [result.length -1].length >= config.neighbors)
return result
}
const step = ({radix, neighbors}) => LUT => (past, index) =>
LUT [past.slice(index, index + neighbors).reduce (
(prev, cur) => prev * radix + cur, 0
)]
const buildLUT = ({radix, neighbors}) => ruleNum =>
(new Uint8Array (neighbors ** radix)).map ((val, idx) =>
(ruleNum / radix ** idx) % radix
)
const generation = ({neighbors}) => stepFun => past => {
let result = new Uint8Array (past.length + 1 - neighbors)
let index = 0
while (index < result.length)
result [index] = stepFun (past, index++)
return result
}