Last active
September 19, 2018 10:06
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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