neith
Version:
Javascript Zipper Library
63 lines (47 loc) • 1.41 kB
JavaScript
var zipper = require('../index').zipper;
var tree = require('../index').tree;
var walk = require('../index').walk;
var stream = require('nu-stream').stream;
var Nary = function(value, children, childValues) {
this.value = value;
this.children = children;
this.childValues = childValues;
};
Nary.construct = function(x, edges, children) {
console.log('x', x.value);
var c = stream.toArray(edges);
return new Nary(x.value,
c.map(tree.pairKey),
children());
};
var naryZipper = function(root) {
return tree.treeZipper(
function(x) { console.log('c', x.value); return stream.from(x.children); },
function(x, k) { console.log('g', x.value); return x.childValues[k]; },
Nary.construct,
root);
};
var $n = function(val, children) {
return new Nary(val, Object.keys(children), children);
};
var nary1 = $n(1, {
2: $n(2, {
3: $n(3, {}),
4: $n(4, {
5: $n(5, {}),
}),
}),
6: $n(6, {
7: $n(7, {}),
}),
8: $n(8, {})
});
var id = function(x) {return x; };
exports.test = function(test) {
tree.node(zipper.root(
walk.walk(id, id, zipper.down(naryZipper(nary1)))));
console.log('__');
tree.node(zipper.down(zipper.right(zipper.down(zipper.root(
zipper.down(zipper.right(zipper.down(naryZipper(nary1)))))))));
test.done();
};