4 ms·
Well, I think that's analogous to the max function having signature (say) Array<Int> -> Optional<Int>
by zodiac 6y ago
Well, I think that's analogous to the max function having signature (say) Array<Int> -> Optional<Int>
- jeff-davis 6y agoBut why is the SUM of no elements NULL, but the COUNT-star of no elements zero? Sure, for any given case you can come up with some explanation. But there is no general explanation that covers many cases. And that leads to weirdness and surprises and bugs. Option types are way better and always do what you expect.
- christophilus 6y agoBecause 0 is a valid sum for non empty sets, so it’s nice to be able to differentiate between: the items add up to 0 and: there are no items to sum.
- petters 6y agoIt is useful to have the sum and product of empty lists be 0 and 1, respectively. This means that sum(A ++ B) = sum(A) + sum(B). (++ is concatenation)
- magicalhippo 6y agoAnd you can have that by using COALESCE(SUM(A), 0) for example.
- heavenlyblue 6y agoBut COUNT is just a SUM over ones for every row in the output
- tsimionescu 6y agoBy what definition? If anything, sums are defined as cardinalities of sets, rather than defining cardinalities in terms of sums.
- heavenlyblue 6y agoThen what is a NULL cardinality?
- zodiac 6y ago> Option types are way better and always do what you expect. No, I can implement the exact same weird functions with option types, with signatures sum: Array<Int> -> Optional<Int> count_elements: Array<Int> -> Int FWIW, I personally prefer both functions to not return Optionals, but the point is that the mere presence of Option types in your type system (and the absence of null pointers) doesn't magically eliminate bad library design
- cerved 6y agoimho, aggregation on []<x> should return option<x> unless aggregation on an empty list is exceptional in which case it may throw
- tsimionescu 6y agoWhat else would the sum/min/max/avg of an empty list be? What else would the count of an empty list be? What is the sum of an empty list in Haskell? What is its length?
- anonymoushn 6y ago0 and 0 in Haskell