Published
Edited
Nov 13, 2019
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Dot = typeClass('Dot', {
toDot: () => { throw new Error(); }
});
Insert cell
_ = {
const {Tree, Node, Empty} = newType('Tree', {
Node: (key, left, right) => ({key, left, right}),
Empty: () => {}
});
// getEdgeString :: Tree a => a -> Tree a -> String
// assumption: a has Show
const getEdgeString = (key, child) => match({
[Node]: ({key: childKey}) => `${key.show()} -> ${childKey.show()}`,
[Empty]: () => {}
})(child);

const getEdges = match({
[Node]: ({key, left, right}) => {
const leftEdge = getEdgeString(key, left);
const rightEdge = getEdgeString(key, right);

const edges = [];
if (leftEdge) { edges.push(leftEdge); }
if (rightEdge) { edges.push(rightEdge); }

return edges.concat(getEdges(left), getEdges(right));
},
[Empty]: () => []
});
const toDot = tree => `digraph { ${getEdges(tree).join('; ')} }`;
instance(Tree, Dot, {toDot});

return {Tree, Node, Empty};
}
Insert cell
Insert cell
Insert cell
Insert cell
{
Show; // force Observable to define Show instance for Number

const myTree =
Node(1,
Node(2,
Node(3, Empty(), Empty()),
Node(4, Empty(), Empty())
),
Node(5, Empty(), Empty())
);

return dot`${myTree.toDot()}`;
}
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