Live data from Hacker News

Why Operators Are Useful

neopythonic.blogspot.com

151–160 of 173 posts

Re: Why Operators Are Useful

#151

I've grown to dislike operator overloading quite a bit, and thus developed a general skepticism with regard to operators in general, and I think this article actually reveals why partially. First off, I'm surprised this article doesn't touch at all on the fact that operators are usually the only "blessed" functions in languages to be able to be written in infix form, as I think that's actually where many of the "bene…

The best part is that string concatenation is a perfectly cromulent "multiplication", forms a free monoid, and in line with some notations for concatenation of tuples. Funny the associations we make...

It also exponentiates the way you'd expect:

  "a"~"a"~"a" = "aaa" = "a"^3
And while it's not commutative, it is anticommutative with reverse:

  -"abc" = "cba"
  "ab"~"cd" = -(-"cd" ~ -"ab") \
  = -("dc"~"ba") = -"dcba" = "abcd"

Re: Why Operators Are Useful

#152

I'm amazed (and horrified) how come such an intelligent mathematician and designer of a beautiful programming language made such an obvious fuckup of using a commutative operator for string concatenation. Really, I hate python just for this single idiotic notation. I mean, it's right there in front of your eyes. He talks about the convenience of using a visually commutative operator like "+" for commutative operation…

Julia does this with *. IMO it weirds people out

With stardot or just star? Yeah either would be a little weird.

I'm asking partly because Ocaml uses stardot for floating point multiplication.

Re: Why Operators Are Useful

#153

I'm amazed (and horrified) how come such an intelligent mathematician and designer of a beautiful programming language made such an obvious fuckup of using a commutative operator for string concatenation. Really, I hate python just for this single idiotic notation. I mean, it's right there in front of your eyes. He talks about the convenience of using a visually commutative operator like "+" for commutative operation…

I for one feel that + is a very intuitive choice for concatenating strings and lists. Is there any other common operator you would recommend instead? Because I feel that using a less common one would harm beginner-friendliness significantly. Also, to the best of my knowledge there is no place in the bible of maths that defines + to be commutative. In particular, addition of (infinite) ordinals is not commutative.

> Is there any other common operator you would recommend instead?

Yes, wedge product[0], written as tilde (~)[1]:

  "a"~("b"~"c") = "abc" = ("a"~"b")~"c" # associative
  "a"~"a"~"a" = "aaa" = "a"^3 # exponentiates with repeat
  -"abc" = "cba" # (with - as string reverse)
  "ab"~"cd" = -(-"cd" ~ -"ab") # weakly[2] anticommutative
If you generalize to regular expressions, you can get some more milage out of + as alternation and * as intersection.

0: https://en.wikipedia.org/wiki/Wedge_product

1: Not caret (^), because that makes a good exponentiate operator.

  2: # vector wedge product is strongly anticommutative
  u~v = -(v~u) = -(-v~-u) # with vector negation

Re: Why Operators Are Useful

#154

Earlier quoted context omitted.

> I for one feel that + is a very intuitive choice for concatenating strings and lists. Is there any other common operator you would recommend instead? Literally, any other operator would be better than addition. A space, a dot, a product, two dots, a minus sign, whatever. Anything except the visually commutative plus. > Because I feel that using a less common one would harm beginner-friendliness significantly. Usage…

“A”+”B” what do you feel should happen? “A”*6 what do you feel should happen?

  "A"+"B" = Regex("A|B") = Regex("[AB]") # or maybe
  "A"+"B" !> [?]Error: cannot convert /A|B/ to type str
  "A"*6 !> TypeError:  * 

Re: Why Operators Are Useful

#155

Earlier quoted context omitted.

Julia does this with *. IMO it weirds people out

With stardot or just star? Yeah either would be a little weird. I'm asking partly because Ocaml uses stardot for floating point multiplication.

Just *

"a" * "b" == "ab"

"a" ^ 3 == "aaa"

Though the dot version is actually almost possible because of a special syntax in Julia called broadcasting, which applies any function element-wise.

"a" .* ["a", "b", "c"] == ["aa", "ab", "ac"]

["a", "b", "c"] .* "a" == ["aa", "ba", "ca"]

("a" * ["a", "b", "c"] would raise an error)

Re: Why Operators Are Useful

#156

Earlier quoted context omitted.

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…

They're the standard forms of doing dict merge in python, and generally the first answer whenever someone asks (both overwrite with right dict key/value on collision, concatenate keys otherwise).

{ * * d1, * * d2} -- unpack the two dicts and feed them both as input to dict comprehension to construct a new dict.

d1.update(d2) does the same merge strategy, but modifying d1 in-place.

Sure, there's five different ways to go about handling collisions, but there's already a well-defined, commonly used methodology, so it's fair to implement syntax sugar for it. It's also not too difficult to understand, and easy to look up (particularly compared to {* * d1,* * d2}).

And I'm not actually aware of any other merge strategy being included in the python stdlib, which implies that this strategy being the best useful default, has been decided long before this operator came into question.

Re: Why Operators Are Useful

#157

Earlier quoted context omitted.

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 wh…

I could be wrong, but I think the reason why String.length is one is to have a consistent idea of what happens when you have monospaced console output. Things in the elixir standard library exist "when you need them for elixir itself", and monospaced console output formatting working is needed in a few parts of elixir. If you care about bytes only, you can use byte_size, as indicated in the docs.

Re: Why Operators Are Useful

#158
post #8

Operators can certainly be nice. And I do like allowing the programmer to create new ones too, though I'd tend to prefer the Haskell approach of creating new ones out of existing symbols like >< or such rather than the C++ approach of letting the programmer redefine an existing operator for a new use, << in the streams library being one of the worst common examples.

Python discourages using operators for things that don't have anything to do with their original use. If you try to be too clever with it, you run into issues. For instance, comparison operators are chained, so that `x > y > z` is equivalent to `x > y and y > z`, not `(x > y) > z`. The most radical use of operators in the standard library that I know of is in pathlib, where `Path('/usr') / 'lib' == Path('/usr/lib')`,…

> Python discourages using operators for things that don't have anything to do with their original use.

Python doesn't really discourage anything and especially not overloading operators in weird ways. You even pointed out the pathlib insanity in the stdlib. Python isn't the shiny bastion of consistency and obviousness that the zen claimed it was years ago

Re: Why Operators Are Useful

#159

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

Surely any new syntax (like +) would also require python3, so that's a bit of a moot point...

Re: Why Operators Are Useful

#160

Earlier quoted context omitted.

Thats not obvious and breaks a commutative property. This observation about operators seemed poorly crafted to support a decision he already made. The existing example was unnecessarily complicated...most languages just do a singular function with a return val. Why be obtuse in the example? Preconceived agenda. The operator isnt compelling for dicts/hashmaps in any language.

Lots of operators are non-commutative. You can't "break" a commutative property that doesn't exist because the operator is not yet defined.

> You can't "break" a commutative property that doesn't exist because the operator is not yet defined.

That's the point (eg modulus). There's an entire preamble that's not relevant to the decision. The coverage of the inane "insights" is some hero worship hype.

Post reply on HN