Skip to content

Instantly share code, notes, and snippets.

@jmakeig
Last active February 27, 2020 07:37
Show Gist options
  • Save jmakeig/a47255f4e43e3a9351d828e42305858a to your computer and use it in GitHub Desktop.
Save jmakeig/a47255f4e43e3a9351d828e42305858a to your computer and use it in GitHub Desktop.
MarkLogic’s cts.frequency function is magic. Could we do something more explicit? (Spoiler: probably not by much)
// https://github.com/jmakeig/iterant/issues/30
function head(itr) {
if (itr[Symbol.iterator]) return itr[Symbol.iterator]().next().value;
}
function* values(ref, options) {
const sequence = cts.values(ref, options);
for (const value of sequence) {
yield {
value: value.valueOf(),
// Lazily call cts.frequency()
get frequency() {
return cts.frequency(value);
},
// Used in default coercion
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
valueOf() {
return value.valueOf();
}
};
}
}
// Usage:
// const { value, frequency } = head(values(tag));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment