Live data from Hacker News

Why Operators Are Useful

neopythonic.blogspot.com

141–150 of 173 posts

Re: Why Operators Are Useful

#141

Earlier quoted context omitted.

Of course it’s not the same. There is one sensible result expected when adding two lists or strings together (since they’re linear, it would not make sense to encourage inefficient search/replace operations with an operator; and also, the containers do not require unique keys, you can simply extend them). A dict must deal with conflicting keys, and arguably each situation requires different treatment of those keys.

Given the semantics of {}.update({}) and { * * {}, * * {}}, it is clear what is the preferred behavior, or at the very least the most commonly known one in Python. Hence there is nothing hard or mysterious what the + operator should do. People are nit picking on this.

> Given the semantics of {}.update({}) and { * {}, * * {}}, it is clear what is the preferred behavior,*

Sorry, I honestly don't understand what's the meaning of that (I know a little bit of Python, C/C++, ASP.Net, etc...).

Concerning adding dicts: thinking over it I am more or less neutral as I think that somebody that would dare doing it would feel the pressure of reading the docs to know what the operator would do (e.g. merge distinct keys & overwrite-left/concatenate/ignore-right values of duplicate keys?).

On the other hand in Python I keep trying from time to time to e.g. cast "integer"s into "bytes" using "bytes(my_integer)", which results in a "my_integer"-bytes variable initialized to zeroes (ha-ha), so whatever would be implemented to add dicts using an op might be good for a large part of users but at the same time bad for the other part (that are inexperienced, have a different ways of thinking, etc...) => this might in turn weaken the language's acceptance.

Re: Why Operators Are Useful

#142
post #23

Earlier quoted context omitted.

This is a non-problem in every language with variadic functions. "add(x, y, z)" looks good to me.

You have to be careful though. In Julia, there is "fma(a,b,c)" which is not necessarily the same as "muladd(a,b,c)". What are the guarantees on add(a,b,c) versus a+b+c? If do add(1.0e30, 1.0, -1.0e30) is that the same (aka IEEE fp addition is noncommutative)

> If do add(1.0e30, 1.0, -1.0e30) is that the same (aka IEEE fp addition is noncommutative)

You might be thinking of associativity. Both addition and multiplication are commutative up to NaN.

Re: Why Operators Are Useful

#143

The dict example given is exactly why operators wouldn't be useful to me. Presumably d1 + d2 != d2 + d1 for some d1, d2. If you're going to use the + operator then it should behave like it does in other contexts.

this is equally untrue true for strings, of course. “foo”+”bar” != “bar” + “foo” there are valid non-commutative additions.

An interesting example is C's pointer-integer addition. p + n == n + p, true, but this is a purely syntactic fact. The actual semantic question of commutativity, whether switching the order of the arguments leaves the value unchanged, cannot even be asked of pointer-integer addition since the arguments, having differing types, cannot be switched.

Re: Why Operators Are Useful

#144
post #81

Earlier quoted context omitted.

If a+b != b+a that breaks the commutativity property of addition.

Commutatitivity does not need to always hold. Multiplication is not commutative on matrices, but we still use it often and write it as a product nonetheless

In mathematics, the ‘multiplication’ operation is not assumed to be commutative, whereas the ‘addition’ operator is used specifically to indicate commutativity. So, for example, using the plus sign for string concatenation goes against this tradition.

Re: Why Operators Are Useful

#145

Earlier quoted context omitted.

Indeed, Julia has the most well-thought-out commentary on strings in their docs I've seen from a non-toy language: https://docs.julialang.org/en/v1/manual/strings/#man-concate...

Elixir's string module documentation isn't half bad, but there is more practical Unicode thinking and less mathematical thinking (which makes sense, given their domains). https://hexdocs.pm/elixir/String.html

I happen to think baking unicode into your concept of a string is fundamentally misguided, so that all string operations following from that premise are inherently wrong. The very first example, constrasting encoded byte length with String.length("é")=1, calling the latter the "proper length" walks into a shibboleth which puts Elixir on the side of String.length("ﷺ")=1, even with the grapheme clusters concept, for which the only salvation is integrated font rendering.

It's practical and informative, but I can't consider it well-thought-out.

ed: to clarify, ﷺ is an Arabic ligature which represents many more than one (linguistic) characters. A more accessible example might be "ffi".

Re: Why Operators Are Useful

#146
post #81

Earlier quoted context omitted.

If a+b != b+a that breaks the commutativity property of addition.

Commutatitivity does not need to always hold. Multiplication is not commutative on matrices, but we still use it often and write it as a product nonetheless

Python actually supports a special operator @ for non-commutative matrix products.

Re: Why Operators Are Useful

#148
post #52

Earlier quoted context omitted.

That’s semantically not an “add”, it’s a “merge” or “update”

What happens when you “merge” a pile of six pennies with a pile of three pennies? You get a pile of nine pennies. Six plus three is nine. Adding three pennies to six pennies results in a single group of nine pennies. My point: don’t just pick words, explain why those words were chosen.

Penny piles can’t be merged together and remain pennies. If you merge a pile of 6 and a pile of 3, you get one metal lump. I actually really like the point you are making, just feel like your illustration works against you.

Re: Why Operators Are Useful

#149

JavaScript deals with this well. The equivalent of the proposed d3 = d1 + d2 is let d3 = {...d1, ...d2}; In fact, it seems like you can already do this in python: d3 = {**d1, **d2} This seems to have most of the benefits of being a dedicated syntax (rather than just a function call), without the downsides of breaking the commutativity of the + operator.

Requires Python3

Re: Why Operators Are Useful

#150

JavaScript deals with this well. The equivalent of the proposed d3 = d1 + d2 is let d3 = {...d1, ...d2}; In fact, it seems like you can already do this in python: d3 = {**d1, **d2} This seems to have most of the benefits of being a dedicated syntax (rather than just a function call), without the downsides of breaking the commutativity of the + operator.

From https://www.python.org/dev/peps/pep-0584/#current-alternativ...

> To create a new dict containing the merged items of two (or more) dicts, one can currently write:

  {**d1, **d2}
> but this is neither obvious nor easily discoverable. It is only guaranteed to work if the keys are all strings. If the keys are not strings, it currently works in CPython, but it may not work with other implementations, or future versions of CPython.

> It is also limited to returning a built-in dict, not a subclass, unless re-written as MyDict(d1, d2), in which case non-string keys will raise TypeError.

Post reply on HN