Public
Edited
Mar 23, 2024
6 stars
Insert cell
Insert cell
Insert cell
function mulberry32(a) {
return function () {
var t = (a += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
Insert cell
Insert cell
rules = ({
E: [["I"], ["M", "*", "M"], ["E", "+", "E"]],
M: [["I"], ["M", "*", "M"], ["(", "E", "+", "E", ")"]],
I: [["-1"], ["1"], ["0"]]
})

Insert cell
Insert cell
weightedRules = ({
E: [/* 2 extra I*/ ["I"], ["I"], ["I"], ["M", "*", "M"], ["E", "+", "E"]],
M: [
/* 2 extra I*/ ["I"],
["I"],
["I"],
["M", "*", "M"],
["(", "E", "+", "E", ")"]
],
I: [["-2"], ["-1"], ["0"], ["1"], ["2"]]
})
Insert cell
function sample(symbol, rules, rng, maxDepth = 20) {
if (maxDepth == 0) throw new Error("max depth reached");
const options = rules[symbol];
if (!options) return symbol;
const choice = options[Math.floor(rng() * options.length)];
return choice.map((child) => sample(child, rules, rng, maxDepth - 1));
}
Insert cell
sample("I", weightedRules, mulberry32(2))
Insert cell
sample("E", weightedRules, mulberry32(Math.random() * 10000))
Insert cell
Insert cell
function traverse(tree) {
if (Array.isArray(tree)) return tree.map((child) => traverse(child)).join("");
else return tree;
}
Insert cell
Insert cell
Insert cell
testRules = ({
C: [["EN", "OP", "EN"]], // Conditional
EN: [
// Variable carrying expression cannot be multipled by other variables, so the expressions remain linear
["I"],
["V"],
["I"],
["V"],
["MN", "*", "M"],
["M", "*", "MN"],
["EN", "+", "EN"]
],
E: [["I"], ["I"], ["I"], ["M", "*", "M"], ["E", "+", "E"]],
MN: [
["I"],
["V"],
["I"],
["V"],
["M", "*", "M"],
["(", "EN", "+", "EN", ")"]
],
M: [["I"], ["I"], ["I"], ["M", "*", "M"], ["(", "E", "+", "E", ")"]],
V: [["x"], ["y"]],
I: [["-2"], ["-1"], ["0"], ["1"], ["2"]],
OP: [["<="], ["=="], [">="]]
})
Insert cell
Insert cell
Insert cell
Insert cell
result = extract_latest(expression)
Insert cell
{
result;
mutable seed = seed + 1;
}
Insert cell
Insert cell
import { footer } from "@tomlarkworthy/footer"
Insert cell
footer
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more