Stream: cql
Topic: Mode
Erich Schulz (Jul 10 2016 at 09:03):
bit worried about the "Mode" operation @Bryn Rhodes - it is polymorphic in that it can return either a list or a single item... seems like a bit of an outlier...
Erich Schulz (Jul 10 2016 at 09:04):
ie this is my current implementation:
function mode(values, defaults) {
if (!values.length) return null
var clusterFunction = function(a) { return a.toString() }
var max = 0, mode = []
var counted = _.countBy(values, clusterFunction)
max = _.max(_.values(counted))
_.forIn(counted, function(v,k) {
if (v === max) mode.push(parseFloat(k))
})
formalise(mode, defaults) // add in type etc
var res = mode.length > 1
? {value: mode, type: 'List'}
: mode[0]
return res
}
Bryn Rhodes (Jul 12 2016 at 04:08):
Yeah, that's a good question. I would tend to argue that it should be the same return type, always returning a list of elements, even if there is only a single result. That's a change to the spec though, the spec actually defines it as returning a single value, and the behavior for ties is unspecified. That should be made explicit in the spec. Do you mind submitting a comment and we can discuss it on the next CDS WG call?
Last updated: Apr 12 2022 at 19:14 UTC