Skip to content

Instantly share code, notes, and snippets.

@derms
Last active September 19, 2018 10:06
Show Gist options
  • Save derms/a58c53289ed68a9ef9c3738393ec7b25 to your computer and use it in GitHub Desktop.
Save derms/a58c53289ed68a9ef9c3738393ec7b25 to your computer and use it in GitHub Desktop.
Extending MarkLogic Sequences with array functions (based on https://gist.github.com/jmakeig/6898dfa2af428a230101f21b60fd047e and https://github.com/jmakeig/iterant)
Sequence.map = function*(iterable, fct, that) {
if ('function' !== typeof fct) {
throw new TypeError('fct must be a function');
}
for (const item of iterable) {
yield fct.call(that || null, item);
}
};
Sequence.prototype.map = function(fct, that) {
return Sequence.from(Sequence.map(this, fct, that));
}
Sequence.reduce = function(iterable, fct, init) {
let value = init, index = 0;
for (const item of iterable) {
value = fct.call(null, value, item, index++, this);
}
return value;
};
Sequence.prototype.reduce = function reduce(reducer, init) {
return Sequence.reduce(this, reducer, init);
};
Sequence.prototype.filter = function filter(predicate, that) {
return Sequence.from(Sequence.filter(this, predicate, that));
};
Sequence.filter = function*(iterable, predicate, that) {
if ('function' !== typeof predicate) {
throw new TypeError('predicate must be a function');
}
let index = 0;
for (const item of iterable) {
if (predicate.call(that || null, item, index++, iterable)) {
yield item;
}
}
};
declareUpdate();
xdmp.documentInsert("/store1.json", {"active":true,"count":7,"location":"Paris"})
xdmp.documentInsert("/store2.json", {"active":false,"count":9,"location":"Paris"})
xdmp.documentInsert("/store3.json", {"active":true,"count":12,"location":"Paris"})
cts.search("Paris")
.filter(doc => doc.root.active == true)
.map(doc => doc.root.count)
.reduce((accumulator, count) => accumulator + count, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment