You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using MinBy like this results in 8 calls to computeAge where 5 should have sufficed. Using a custom keying function with MinBy results in many more calls than necessary.
I propose adding a MinByKey and MaxByKey functions that apply the custom keying function to all elements and then return the one that minimizes/maximizes it.
The signatures would be:
funcMinByKey[Tany, K constraints.Ordered](collection []T, keyfunc(T) K) TfuncMaxByKey[Tany, K constraints.Ordered](collection []T, keyfunc(T) K) T
Say you have a collection and want to find the element that minimizes or maximizes a function.
For example, here I have a slice of
person
structs and am looking for the one for which thecomputeAge
method returns the lowest value:Using
MinBy
like this results in 8 calls tocomputeAge
where 5 should have sufficed. Using a custom keying function withMinBy
results in many more calls than necessary.I propose adding a
MinByKey
andMaxByKey
functions that apply the custom keying function to all elements and then return the one that minimizes/maximizes it.The signatures would be:
Would this contribution be appreciated @samber ?
The text was updated successfully, but these errors were encountered: