More likely they install a 3rd party generic sum function and don't have to write their own at all. Well, in the case of a sum function it's probably only 15 minutes to write to generic version, and there may even be one in the standard library. But consider something like a concurrent hash map. In Rust, You just `cargo add dashmap` and you have one that works with any type that works with a regular hashmap. This isn't possible without generics.
> More likely they install a 3rd party generic sum function and don't have to write their own at all. Well, in the case of a sum function it's probably only 15 minutes to write to generic version
I'm not sure "sum over an array of int" is a great "simple" example - how do you guarantee that the result can fit in an int?
This is (one reason) why Julia implemented a full, scheme-style, numeric tower right in the core language.
Sure there are some examples where you can sum N Mbit numbers into an Mbit number, and either want overflow, or is somehow "confident" things will fit. But in general it's just lazy "no one will ever have such a large shopping cart"-design...
What's the result of adding two i32 then? Is it an i64? Doesn't the accumulator need to get wider on some additions, since adding an i64 to an i32 might also overflow an i64?
0 + typemax(UInt8)+ typemax(UInt8)
510
While adding UInt8 first, then an implicit Int64 causes overflow: typemax(UInt8)+ typemax(UInt8)
254
(ie: 255 overflows to 254 - or is promoted and sums to 510)See: https://docs.julialang.org/en/v1/manual/integers-and-floatin...
The blub paradox https://wiki.c2.com/?BlubParadox
This is the essence of the blub paradox, thinking that people who use a feature are simply misusing it, after all in blub you don't have generics and things are fine. They're just making things more complicated than they need to be.
The blub programmer isn't necessarily smug, they just make excuses to dismiss more powerful language features that they are not sufficiently familiar with.
I don't know how to resolve disputes over the relative power of programming languages on that basis. Are dynamic languages more or less powerful than static languages with advanced type systems? I don't know. What's the definition of power?
But I think what we can take from blub is that familiarity with one language or set of features is an insufficient basis for dismissing other, unfamiliar, languages or features.
A good class or generic is hard to beat. Unfortunately the understanding of the details that make something a good class requires experience and experiments. Maybe I am talking about premature optimization, writ larger, or in design.
template<class T>
T sum(T* x, size_t size)
{
T acc = 0;
for(size_t i = 0; i < size; ++i)
acc += x[i];
return acc;
}Took me five minutes, not four days…
Polymorphism. But note that there is only one function of type 'a->'a for all 'a, and that's the identity function.
def nitpick(x): throw “foo”
technically a partial function, or you could say that every function is really of the type A -> Exception \/ A
func sum[Number](nums: openArray[Number]): Number =
for num in nums:
result += num
Took about a minute.I'm sorry. I had to do it. Ban me.
There's something like a cognitive bias that makes me leary of genetics (in any language). Once you have some cross cutting feature, generics or object orientation, or in functional programming, those functions of type 'a->'a, or assembly language address modes, people get bogged down by trying to make everything in their work generic or object oriented or "orthogonal". Lemire's example is relevant to his topic, but it could be a poster child if someone else did it. They need to sum an array of int. So they spend 4 days getting a function and a generic type that can sum every numerical type, rather than 15 minutes to sum int arrays.
What's this called?