Live data from Hacker News

Why Operators Are Useful

neopythonic.blogspot.com

71–80 of 173 posts

Re: Why Operators Are Useful

#71
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')`,…

You should look at the Construct library, which overloads the '/' operator as syntactic sugar to make s-expressions in it's DSL less paren-heavy. I'm not saying I agree, but it was wild when I first saw it.

    Struct(
        "foo" / byte,
        "bar" / Struct( 
            "spam" / int16ul, 
            "bacon" / int64sb, ), 
        "viking" / int32sl, )

Re: Why Operators Are Useful

#73
post #64
post #2

This is much less confusing than (2), and leads to the observation that the parentheses are redundant, so now we can write "x + y + z" This is a non-problem if you're using Lisp. (+ x y z) accepts an arbitary number of arguments and the operator precedence problem does not exist since there is no operator precedence.

Try writing out typical mathematical formula derivations using only s-expressions. I tried for a period and abandoned the persuit. It’s just not comparable to established mathematical notation.

Two solutions. Threading macros wherein instead of nested parens like (x (y (z))) one writes

    ((->> z)
          y
          x)
In clojure there is an interesting package https://github.com/rplevy/swiss-arrows which allows one to perform successive operations with explicit placement of the result of prior evaluation by placing a in the form

    ((->> (z ))
          (y  7)
          (x ))
In practice it seems like there is often less need to do so as many similar functions or the same variety have the same ordering and other options like as-> exist too.

There is also the idea of processing math expressions infix as expected when desired.

https://github.com/rm-hull/infix

    ($= 3 + 5 * 8)
    ; => 43

Re: Why Operators Are Useful

#74
post #59

But, this argument defeats itself. At least, in practice. If I look at how 'operator overloading' is used in practice, _rarely_ do you get commutativity, or even anticommutativity, associativity, or distributivity. Take list addition, where operator overloading often shows up: someList += someElement is shorthand for: someList.add(someElement) add is not commutative; the elements used in the operation aren't even the…

In my opinion, requiring commutativity from a +-operator is maybe too much. Associativity and a neutral element seem to be enough (i.e. forming a monoid).

I think that just shows that the python language and some others la k another operator and not that + should not be commutative.

Re: Why Operators Are Useful

#75
post #69

Earlier quoted context omitted.

Ramblings through technology, politics, culture and philosophy by the creator of the Python programming language. This was literally the first thing I read when I hit the link. Don't mean to sound rude but how did you miss that ? Does it render differently on web? I saw this on the mobile version of the site on my Android.

For me that was covered up by some popover I didn't read. Besides the tagline of a blog is the sort of thing I for one gloss over. I've had decades of training how to skip straight to the content.

That's what I thought, usually even I am trained to skip those few lines under titles (like opinion articles have a blurb about the author )

Re: Why Operators Are Useful

#76

Wow, he's still got it. I read the whole thing, without realizing it was Guido Rossum, thinking "hey, that's a really good point". Then got to the end and realized it was the inventor of the Python language. So, I wasn't impressed as I was reading it because of who it was, I was impressed because he was making a really good point. Obviously, the fact that he made my favorite programming language was no fluke.

Ramblings through technology, politics, culture and philosophy by the creator of the Python programming language. This was literally the first thing I read when I hit the link. Don't mean to sound rude but how did you miss that ? Does it render differently on web? I saw this on the mobile version of the site on my Android.

I don't know why, but I did the same thing. I read the post content first, then after finished I went backup and saw the blog title and subtitle.

Or I could say my brain was only conscious to reading the blog title and subtitle afterwards.

Re: Why Operators Are Useful

#77

Earlier quoted context omitted.

> even for dict, it's just a matter or reading from the left to the right. How's that? There's no defined "left to right" for a dict.

Vales of keys in the left operand are overwritten by values of keys in the right operand.

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.

Re: Why Operators Are Useful

#80

Earlier quoted context omitted.

What? Have you never had two dicts and wanted vals from one dict override the other?

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

Yes, but I still want this for combining dictionaries. Python already violates this property, I don't find it confusing.

    Python 3.7.2 (default, Dec 30 2018, 08:55:50) 
    [Clang 10.0.0 (clang-1000.11.45.5)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 'foo' + 'bar' == 'bar' + 'foo'
    False
    >>> ['foo'] + ['bar'] == ['bar'] + ['foo']
    False
Post reply on HN