3 ms·
My two mains "why tf is this not built-in" are: "Why are tuple not Hashable ?!" Which means that you cannot have a tuple of Int `(1, 1)` as a key in a dict, a
by alain_gilbert 1y ago
My two mains "why tf is this not built-in" are:
"Why are tuple not Hashable ?!"
Which means that you cannot have a tuple of Int `(1, 1)` as a key in a dict, and you have to make a struct that implement Hashable instead.
And
"Why do they not have a `.sum()` and I have to do `.reduce(0, +)` every time instead."
Or implement it yourself
extension Sequence where Element: AdditiveArithmetic {
func sum() -> Element { reduce(.zero, +) }
}
- frizlab 1y agoI think tuples will be (soon-ish) Hashable but cannot as of now because they want to do it right and need something before they can do it.
- zffr 1y agoWhy do you feel the stdlib needs a .sum() if using .reduce() is so simple?
- alain_gilbert 1y agoBecause it's annoying to have to implement it every time you make a one off script (or small projects) among other things. And it is usually a pretty basic iterator standard to have it so that someone maybe not as familiar with concepts like reduce can just sum the sequence. Or if someone not familiar with reduce read your code, and is like "what is that `.reduce(0, +)` thing everywhere!?" while instead it should be a no brainer `.sum()` <-- "yup make sense" https://doc.rust-lang.org/std/iter/trait.Sum.html https://doc.rust-lang.org/std/iter/trait.Sum.html