Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
felixroos committed Aug 26, 2024
1 parent 2b781d9 commit 677c6fc
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,28 @@
return fn(this);
};

// TODO: dedupe with dfs
function visit(node, visited = []) {
Node.prototype.dfs = function (fn, visited) {
return this.apply((node) => dfs(node, fn, visited));
};

let dfs = (node, fn, visited = []) => {
node = fn(node, visited);
visited.push(node);
node.ins.forEach((child) => {
if (!visited.includes(child)) {
visit(child, visited);
node.ins = node.ins.map((input) => {
if (visited.includes(input)) {
return input;
}
return dfs(input, fn, visited);
});
return visited;
}
return node;
};

function flatten(node) {
const flat = visit(node);
const flat = [];
dfs(node, (node) => {
flat.push(node);
return node;
});
return flat.map((node) => {
let clone = {
...node,
Expand All @@ -122,22 +131,6 @@
});
}

Node.prototype.dfs = function (fn, visited) {
return this.apply((node) => dfs(node, fn, visited));
};

let dfs = (node, fn, visited = []) => {
node = fn(node, visited);
visited.push(node);
node.ins = node.ins.map((input) => {
if (visited.includes(input)) {
return input;
}
return dfs(input, fn, visited);
});
return node;
};

function compile(node, options = {}) {
const {
log = false,
Expand Down

0 comments on commit 677c6fc

Please sign in to comment.