Last active
February 27, 2020 07:37
-
-
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)
This file contains 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
// 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