lo icon indicating copy to clipboard operation
lo copied to clipboard

Implement Min/Max as variadic functions

Open dergus opened this issue 3 years ago • 5 comments

HavingMin/Max functions take a slice as an argument is a bit inconvenient when you have just two variables because then you have to create an array out of them just for calling Min/Max functions. And with variadic version you still could provide a slice if you wished by using the ... operator.

dergus avatar Sep 24 '22 18:09 dergus

Hi @dergus,

For the current available functions, that'd be a breaking change. See an example here. While a variadic parameter may take a slice, you're still required to add ... after the slice argument, making it a breaking change.

We could still consider a second set of functions with the variadic parameters. @samber, if you think that makes sense, I would like to work on that as part of hacktoberfest.

rkperes avatar Oct 08 '22 23:10 rkperes

Hi, I see That's also an option. I have a pr for this but I can reimplement as a separate set of functions

dergus avatar Oct 09 '22 13:10 dergus

Hi @dergus & @rkperes

I agree, but I am reluctant to any breaking change. I will let this issue+PR open until v2.0.0.

samber avatar Oct 11 '22 16:10 samber

I implemented a version in more convenient way, I do not agree to change the interface, because the interface of the base library once opened to change will cause a lot of dissatisfaction.

func MaxN[T constraints.Ordered](items ...T) T {
	return Max(items)
}

func MinN[T constraints.Ordered](items ...T) T {
	return Min(items)
}

If you @samber looks good, please approve it.

dashjay avatar Nov 12 '24 08:11 dashjay

In Go 1.21, built-in min and max functions were introduced, allowing developers to determine the minimum and maximum values among a set of comparable elements without the need for external libraries.

I don't think this needs to be implemented as the language already provides a built-in variadic approach

https://tip.golang.org/ref/spec#Min_and_max

K4L1Ma avatar Jan 28 '25 16:01 K4L1Ma