caio.co/de/go-tdigest

ForEachCentroid should not return value, since this makes assumptions about the behavior of the supplied function.

Id
8140a3fa504070f4d9177e5fa1038582e9868ebb
Author
Andrew Gillis
Commit time
2016-03-23T08:42:39-07:00

Modified tdigest.go

@@ -181,15 +181,14

// ForEachCentroid calls the specified function for each centroid.
// Iteration stops when the supplied function returns false, or when all
-// centroids have been iterated. The number of centroids handled is returned.
-func (t *TDigest) ForEachCentroid(f func(mean float64, count uint32) bool) int {
+// centroids have been iterated.
+func (t *TDigest) ForEachCentroid(f func(mean float64, count uint32) bool) {
s := t.summary
for i := 0; i < s.Len(); i++ {
if !f(s.keys[i], s.counts[i]) {
- return i + 1
+ break
}
}
- return s.Len()
}

func shuffle(data []centroid) {

Modified tdigest_test.go

@@ -244,31 +244,25

// Iterate limited number.
means := []float64{}
- handled := tdigest.ForEachCentroid(func(mean float64, count uint32) bool {
+ tdigest.ForEachCentroid(func(mean float64, count uint32) bool {
means = append(means, mean)
if len(means) == 3 {
return false
}
return true
})
- if handled != 3 {
+ if len(means) != 3 {
t.Errorf("ForEachCentroid handled incorrect number of data items")
- }
- if len(means) != handled {
- t.Errorf("ForEachCentroid did not call processing function")
}

// Iterate all datapoints.
means = []float64{}
- handled = tdigest.ForEachCentroid(func(mean float64, count uint32) bool {
+ tdigest.ForEachCentroid(func(mean float64, count uint32) bool {
means = append(means, mean)
return true
})
- if handled != tdigest.Len() {
+ if len(means) != tdigest.Len() {
t.Errorf("ForEachCentroid did not handle all data")
- }
- if len(means) != handled {
- t.Errorf("ForEachCentroid did not call processing function")
}
}