Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice.Sum should be (more) generic #7

Open
timrprobocom opened this issue Feb 12, 2025 · 0 comments
Open

slice.Sum should be (more) generic #7

timrprobocom opened this issue Feb 12, 2025 · 0 comments

Comments

@timrprobocom
Copy link

slice.Sum is a very useful addition to the toolkit, but I don't understand why you have it returning a float. Wouldn't it be better if the return type matched the type of the slice elements? As it is, you have the potential to lose bits if the slice is int64.

https://github.com/dablelv/cyan/blob/6263b97ab9c2185800674db169ff99a30e958005/slice/get.go#L198C1-L207C1

For example:

func Sum[S ~[]E, E numerical](s S) E {
	var sum E
	for _, v := range s {
		sum += E(v)
	}
	return sum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant