9 ms·
Why operators are useful (2019)
- jevgeni 7y agoPlease, please, please, a pipe forward operator. <3
- ludwigvan 7y agoInteresting point. This might also explain why Lisp never really went mainstream.
- bhaak 7y agoCounterpoint: Java. Java was designed during a time when operator overloading was considered abysmal language design. But that was mostly due to the experiences collected with C++ (or maybe also Perl?).
- saagarjha 7y agoI have dreams of throttling the person who decided that operator overloading was overrated every time I work with BigInteger.
- deniscepko2 7y agoDo you have examples of real world code you had to write with BigInteger? just for amusements sake
- saagarjha 7y agoI can't seem to find the file for it, but I solved #2 on this with Java and wanted to die by the end of it: http://socalcontest.org/history/2017/SC2017ICPCProblems.pdf http://socalcontest.org/history/2017/SC2017ICPCProblems.pdf
- dtech 7y agoIt's one of the things I really like about scala, x call y is always equivalent to x.call(y) This combined with relaxed method name restrictions allow you to do "operator overloading" in quite a sane way and allow you to write bigint + 1 or set contains element. Kotlin is a bit more conservative and only translated the built-in operators to fixed methods: + to the plus method etc.
- gowld 7y agoWrite your program on ints and then transform it with sed before compiling.
- saagarjha 7y agoThat sounds significantly more difficult.
- Avshalom 7y agoThe worst part is there is a talk by Steele called Growing a Language about designing Java where specifically talks about the importance of overloading so that things like future number representations like BigInteger can be first class
- b2gills 7y agoPerl 5.000 came out in 1994, the same year as Java. Perl didn't even have objects before that. So no it wasn't from experience with Perl.
- toolslive 7y agoIt might also be that your brain is just more used (years of extra experience) to infix notation. So why not take advantage of this ? In the monad world, people also prefer (>>=) to bind. OTOH, how long does your brain need to get used to other notations? As an aside, his arguments are completely debunked if you use RPN. > Commutative: a b + == b a + > Associative: a b c + + == a b + c + > ...
- steerablesafe 7y agoWhy do you use infix for ==?
- toolslive 7y agootherwise the challenged would not understand my point ;)
- leethargo 7y agoI thought I might get some anecdotal evidence for this by teaching our daughter Scheme (and prefix notation) before she enters elementary school :-)
- lioeters 7y agoThat sounds great. The syntactic simplicity of prefix notation and Scheme seem particularly suited for early education. I imagine it will help form a intuitive mental model of computation and mathematics, before being taught the classic infix notation. I guess you'll see!
- VMG 7y ago> It might also be that your brain is just more used (years of extra experience) to infix notation. Maybe infix notations is preferred because it aligns more closely to natural language grammar (for some languages)
- steerablesafe 7y ago
- jbverschoor 7y agoWithout operator we’d still be MOVing and ADDing
- saagarjha 7y agoWe might be using Lisp.
- saagarjha 7y agoUnrelated, but I think this is the first time I've seen Mailman with actual CSS applied to it…
- Cthulhu_ 7y agoIs that from a mailing list? I really like it, it's much more readable / user friendly than most mailing list posts I've seen. Call me an inverse luddite or something but I never could get into mailing lists.
- saagarjha 7y agoYup, it looks like it's using HyperKitty.
- Anthony-G 7y agoIt's been about 15 years since I last installed and configured GNU mailman but I thought it was a solid application for what it does. It's great to see UI improvements in the web interface side of it. To save others from having to use a search engine, the project page is at https://gitlab.com/mailman/hyperkitty https://gitlab.com/mailman/hyperkitty with documentation at https://hyperkitty.readthedocs.io/en/latest/ https://hyperkitty.readthedocs.io/en/latest/
- oblio 7y agoThis is the kind of thing that wouldn't hurt if would be contributed back to mailman and became the default. Many of these Open Source projects have really poorly thought out defaults (if anybody even thought of these default values, at all).
- 1wd 7y agoThe PEP adding a matrix multiplication operator to Python also contains a section about the advantage of an operator in that case: https://www.python.org/dev/peps/pep-0465/#why-should-matrix-multiplication-be-infix https://www.python.org/dev/peps/pep-0465/#why-should-matrix-... On the other hand adding a ~ operator similar to R seems was just rejected: https://mail.python.org/archives/list/python-ideas@python.org/thread/GE77ACXLJMLHCTK73MKVKEW47GAVQOHZ/ https://mail.python.org/archives/list/python-ideas@python.or...
- Aardwolf 7y agoI agree, I find it awkward when a programming language (like Java and JS) provide operators for their built in types, but if you want to use different types (say, complex numbers, numbers with different precision, ...) you have to use much less readable functions instead. About the argument that operator overloading can be abused for unreadable code: so can functions, naming, or anything else.
- abjKT26nO8 7y agoI much prefer languages which simply do not make a distinction between functions and operators. This distinction seems useful only in the context of lower-level languages like C where the operators translate directly to single machine code instructions and thus they provide clues about performance. But in a higher-level language this distinction is superficial. It's just another function, just with a much shorter name and perhaps using infix instead of prefix notation. E.g. in Lisps there is no such distinction. "+" is a valid function name. In Haskell - same, but you also have a built-in syntax for specifying that you want your function to be called using infix notation by default.
- bambataa 7y agoIn principle I agree with this. But as a relative beginner to Haskell operators and their precedence levels are a barrier. Operators/functions like `+` have well-understood semantics but I don't see how anyone could have an intuition for what `.:` does. On precedence, I find myself wishing my editor could show me the expression fully parenthesised so I can see if I'm not mentally parsing it correctly.
- gowld 7y agoIf we teach operators to children they won't become adults biased against some operators they haven't learned.
- AnimalMuppet 7y agoSee Silhouette's post that is parallel to the parent. These operators don't make sense until you know the underlying concepts. Teaching the whole thing to children seems ambitious enough to probably be unrealistic.
- mcherm 7y agoSo he's saying that operator notation engages "system 1" mental processing whereas the function call notation requires "system 2".[1] [1] https://en.m.wikipedia.org/wiki/Thinking,_Fast_and_Slow https://en.m.wikipedia.org/wiki/Thinking,_Fast_and_Slow
- seemslegit 7y agoOperators can be useful in a setting where they are expected and natural - numbers, strings etc. Overriding them for dictionaries, streams custom classes has been shown to be a bad idea that makes the authors feel clever but results in code that is both less readable and harder to lookup - your IDE could bring up the right documentation when asked on dict.union but without some advanced syntax and type analysis will not be able to guess what | means for dicts and you'll get the documentation for bitwise or
- gowld 7y agoIt's weird to blame a language design for what's simply a bug in your IDE.
- seemslegit 7y agoIt's not a bug in a specific IDE - it's a language design feature that makes building support tools like IDEs harder. It also goes against Python's own zen of "explicit is better than implicit"
- thomasahle 7y agoThis may have been mentioned elsewhere, but Python already has a syntactic way of merging dictionaries: d = {**d1, **d2} which arguably has all the visual benefits argued for.
- rat9988 7y agod = d1 + d2 is way easier to parse.
- tremon 7y agoAs mentioned elsewhere in this thread, the mathematical + operator is commutative: d1 + d2 == d2 + d1. How does the + operator maintain that commutativity in the face of duplicate keys?
- rat9988 7y agoThe + symbol doesn't denote any particural operation mathematics. it widely used for vector space where commutativity is a condition though. A symbol's property depend on the operation, the multiplication symbol for example is commutative in some cases and not in others.
- aratakareigen 7y agoIt is a convention, not a rule, in mathematics to use (+) to represent operations that are commutative (i.e. abelian) groups. But this convention is not at all respected in programming (e.g. + for string concatenation in many languages) so I have to admit that it's pretty irrelevant.
- NoodleIncident 7y agoThe war to keep programming operators at all consistent with mathematics was lost long ago, with `=` and `==` being the worst offenders.
- bibyte 7y agoFor example take a look at the Python API for Z3. Because of operator overloading it is just beautiful. Much better then raw SMT2. https://ericpony.github.io/z3py-tutorial/guide-examples.htm https://ericpony.github.io/z3py-tutorial/guide-examples.htm
- edflsafoiewq 7y agoSympy is nice too.
- jerome-jh 7y agoOperators are a tradeoff between compactness and ambiguity. They add ambiguity because they are polymorphic, and the order of operations becomes implicit, governed by the operator's priority (for infix or mixed pre/postfix). Many programming languages abuse the + operator for string concatenation, which is not commutative. This is where my tolerance for operators ends.
- LittlePeter 7y agoI can imagine there exists advanced math where addition is also not commutative, yet they probably use `+` there too. Aren't you throwing the baby away with the bath water?
- cygx 7y agoSuch usage may exist, but it should be rare. In the words of Wikipedia, It is conventional to use the plus sign to only denote commutative operations. For possible alternatives, Perl6 uses ~ for string concatenation (and prefix ~ for stringification). I like it.
- lizmat 7y agoPlease note that Perl 6 has been renamed to Raku (https://raku.org https://raku.org using the #rakulang tag on social media). With regards to using ~ , nothing has changed :-)
- enaaem 7y agoI understand your confusion, but in math the + operator does not have to be commutative either. It often is, but just like in programming, you can define in it anyway you want.
- contravariant 7y agoBy convention the + operator is usually reserved for commutative operations though.
- 7y ago
- leethargo 7y agoI like the example about the distributive law. But for the associative law, one could also simply write: add(x, add(y, z)) == add(x, y, z)
- abjKT26nO8 7y agoNo. That assumes that "add" is left-associative, i.e. it expands to add((add(x, y), z)). It's not obvious.
- leethargo 7y agoOK, but for operators that are both left- and right-associative, this notation (variable number of arguments) is already used, e.g. in LISP, right?
- abjKT26nO8 7y agoBoth... at the same time? I.e. they are associative? If only one at a time, then perhaps there is. But TBH I can't think of an example from the top of my head right now.
- leethargo 7y agoYes, I meant (fully) associative.
- abjKT26nO8 7y agoIn this case it's justified, because there is no ambiguity. Just as a mathematician would write using traditional notation without parentheses. At the courses I attended in group theory, module theory etc. we were always required to prove that things are associative before dropping parentheses. Lisp notation just requires less characters (the '+' isn't repeated), but the convention is the same.
- xen0 7y agoThere are two things I hate when dealing with maths heavy code. 1. Lack of operator overloads. 2. Long variable names. The first I find uncontroversial, the second is where I have met with disagreement.
- regularfry 7y agoIt's almost like notation is a tool of thought, or something.
- LittlePeter 7y agoFor the uninitiated, "notation as a tool of thought" is a paper by Ken Iverson (https://m-cacm.acm.org/magazines/1980/8/10988-notation-as-a-tool-of-thought/pdf https://m-cacm.acm.org/magazines/1980/8/10988-notation-as-a-...), creator of APL programming language
- mettamage 7y agoReducing cognitive load [1a], transforming representations into something we can understand (e.g. x86 vs Python) [1b] and engaging our visual cortices [2] are all amazing things to get ourselves to be more productive. [1a] Cognitive psychology: working memory 4 to 10 items [1b] Cognitive psychology: I read it in some textbook how this works well, it also makes intuitive sense to me. [2] This idea was stated out loud in one of 3Blue1Brown's videos, and well, in his case he demonstrates how true it is.
- sswaner 7y agoLeaving the substantive comments to the mathematicians, this was an enjoyable read that was easy to understand. Especially “Of course, it's definitely possible to overdo this -- then you get Perl.”
- keymone 7y agoassumption that ‘add’ is a function of two arguments is wrong if not deliberately misleading
- leethargo 7y agoWhy? In math, that's actually how operators are defined.
- keymone 7y agoBecause you’re limiting expressiveness of the function by applying unnecessary restrictions
- joppy 7y agoSure, so prove that add(x1, x2, ..., xn) is equal to any permutation of the numbers x1, ..., xn. You still need to prove that add(x, add(y, z)) = add(x, y, z). So now you have two things to prove...
- keymone 7y ago“Sure” what? I don’t need to prove any of that to use variadic ‘add’ function. Pretending like it’s function of just two args is just a cherry-picked example to make operators look nicer.
- continuational 7y agoYes, well known operators like `+` with well known meanings are useful. Why would you write a post about this? Is there some kind of disagreement out there about this? I can't think of any popular language that doesn't have it! Now, consider this instead: n <> x &~ n Not only do you not know how to parse this without further knowledge, you also can't pronouce it in a meaningful way or hope to guess what it means.
- csunbird 7y agoIs this legal python?
- ainar-g 7y agoYes. At least in Python 2. "<>" was an alternative spelling of "!=".
- tyingq 7y ago<> is gone in python 3
- JNRowe 7y agoWhen you're unsure about how Python treats an expression, then the ast¹ module is your friend: >>> ast.dump(ast.parse('n != x &~ n'), False) Module([Expr(Compare(Name('n', Load()), [NotEq()], [BinOp(Name('x', Load()), BitAnd(), UnaryOp(Invert(), Name('n', Load())))]))]) It can make it easier to reason through the curiosities, such as the precedence in the given example. Note: Suggesting `ast` as a solution surely proves continuational's point ;) 1. https://docs.python.org/3/library/ast.html https://docs.python.org/3/library/ast.html
- kace91 7y agoAnd it's not easy to Google, which is arguably more important than being pronounceable
- aequitas 7y ago> Not only do you not know how to parse this without further knowledge, you also can't pronouce it in a meaningful way... Therein often lies the problem, if you mentally cannot translate the symbols into meaningful concepts it's hard or impossible to work with them. For example, there are primitive tribes who's language only has words for the amounts of one, two and three. All amounts above that are simply referred to as 'many', no matter if it's four or a hundred. These people cannot (or at least not easily) be thought basic mathematics because they probably can mentally picture 2+2=4 being different from 2+3=5. So if you can find words for these symbols or make up words for your own for that matter and are able to read this expression in these words it will be a lot easier to parse and understand, maybe even intuitively. edit: another example in the Python domain for me would be list and dict comprehensions. When I first encountered them in other people's code I had a hard time understanding what they did. Searching for them was also hard, since I could only describe them (ie: loop within square brackets) and not name them. Finally when I found their name in an reply on SO I was able to search directly for the concepts and find meaningful answers on how to apply them in certain cases.
- msluyter 7y agoSlightly orthogonal to the original point, but worth noting that operators can also increase confusion when they don't interact with each other in expected ways. For example, in Python, 'x += y' isn't equivalent to 'x = x + y' in all contexts. Example: >>> a_tuple = ([1, 2], [3, 4], [5, 6]) >>> a_tuple[2] += ['foo', 'bar'] TypeError: 'tuple' object does not support item assignment >>> a_tuple ([1, 2], [3, 4], [5, 6, 'foo', 'bar']) vs >>> a_tuple = ([1, 2], [3, 4], [5, 6]) >>> a_tuple[2] = a_tuple[2] + ['foo', 'bar'] TypeError: 'tuple' object does not support item assignment >>> a_tuple ([1, 2], [3, 4], [5, 6]) Edited to add, to understand what's happening here, check out the documentation on in place operators: https://docs.python.org/3/library/operator.html#in-place-operators https://docs.python.org/3/library/operator.html#in-place-ope...
- nitnelave 7y agoIn a discussion on the usefulness/overuse of operators, I'm surprised nobody has mentioned J. Here's quicksort in J: (($:@(<#[), (=#[), $:@(>#[)) ({~ ?@#)) ^: (1<#) Good luck understanding that!
- ken 7y ago> For mathematicians, operators are essential to how they think. But programming is not math, just like it isn't text, or color, or datetimes, or any other type of value our programs deal with. Even in programs I've written whose purpose is numerical processing, a vanishingly small fraction of the program involves doing math. We have no problem using a domain-specific language (like regular expressions) for the occasions where we need to work with text. Likewise for querying a database (SQL), or text styling (HTML). We shouldn't need to infect the rest of the language with all the massive complexity of "operators" just to be able to write "a+b" in every possible context. In fact, even when I need to do math, the built-in operators are too weak to support half the math I need to do. We're paying a huge cost, and not getting our complexity's worth. It's not even visually correct. The example here is: x + (y + z) == (x + y) + z which is true in mathematics but not true in most programming languages, where the values could be implicitly promoted modulo integers of different widths, or IEEE-754 floating-point values. He says he wants operators so you can see that addition is associative, but in his language, it's not. My favorite design, in terms of separating the syntax, is actually Tcl. Remove the complexity of needing infix arithmetic operators from 99% of the language, but keep it available for the times when you want it. It still doesn't support half the math I need, but at least the language was kept simple by having math ("expr") and text ("regexp") off in their own commands.
- MaxBarraclough 7y ago> it doesn't matter whether the + operator binds tighter to the left or to the right Depending on the language, (a+b) + c may or may not be the equivalent of a + (b + c) Interesting. Apparently this is true in Python because it uses bignum numbers. As I understand it, it's true in Java too, because (signed) integer overflow is defined to wrap-around. It's not true in C, where signed overflow invokes undefined behaviour. In C# it depends on the mode whether you get wrap-around or an exception on overflow. And of course it's never true with floating-point.