tpl/collections: Add collections.Complement

Fixes #5400
This commit is contained in:
Bjørn Erik Pedersen
2018-11-05 18:50:11 +01:00
parent 47506d1644
commit 42d8dfc8c8
4 changed files with 181 additions and 0 deletions

View File

@@ -41,6 +41,24 @@ func numberToFloat(v reflect.Value) (float64, error) {
}
}
// normalizes different numeric types to make them comparable.
// If not, any pointer will be unwrapped.
func normalize(v reflect.Value) interface{} {
k := v.Kind()
switch {
case isNumber(k):
f, err := numberToFloat(v)
if err == nil {
return f
}
case k == reflect.Ptr:
v = v.Elem()
}
return v.Interface()
}
// There are potential overflows in this function, but the downconversion of
// int64 etc. into int8 etc. is coming from the synthetic unit tests for Union etc.
// TODO(bep) We should consider normalizing the slices to int64 etc.