r/ProgrammingLanguages • ⌘ Noda • Oct 21 '22

Discussion What Operators Do You WISH Programming Languages Had? [Discussion]

Most programming languages have a fairly small set of symbolic operators (excluding reassignment)—Python at 19, Lua at 14, Java at 17. Low-level languages like C++ and Rust are higher (at 29 and 28 respectively), some scripting languages like Perl are also high (37), and array-oriented languages like APL (and its offshoots) are above the rest (47). But on the whole, it seems most languages are operator-scarce and keyword-heavy. Keywords and built-in functions often fulfill the gaps operators do not, while many languages opt for libraries for functionalities that should be native. This results in multiline, keyword-ridden programs that can be hard to parse/maintain for the programmer. I would dare say most languages feature too little abstraction at base (although this may be by design).

Moreover I've found that some languages feature useful operators that aren't present in most other languages. I have described some of them down below:

Python (// + & | ^ @)

Floor divide (//) is quite useful, like when you need to determine how many minutes have passed based on the number of seconds (mins = secs // 60). Meanwhile Python overloads (+ & | ^) as list extension, set intersection, set union, and set symmetric union respectively. Numpy uses (@) for matrix multiplication, which is convenient though a bit odd-looking.

JavaScript (++ -- ?: ?? .? =>)

Not exactly rare– JavaScript has the classic trappings of C-inspired languages like the incrementors (++ --) and the ternary operator (?:). Along with C#, JavaScript features the null coalescing operator (??) which returns the first value if not null, the second if null. Meanwhile, a single question mark (?) can be used for nullable property access / optional chaining. Lastly, JS has an arrow operator (=>) which enables shorter inline function syntax.

Lua (# ^)

Using a unary number symbol (#) for length feels like the obvious choice. And since Lua's a newer language, they opted for caret (^) for exponentiation over double times (**).

Perl (<=> =~)

Perl features a signum/spaceship operator (<=>) which returns (-1,0,1) depending on whether the value is less, equal, or greater than (2 <=> 5 == -1). This is especially useful for bookeeping and versioning. Having regex built into the language, Perl's bind operator (=~) checks whether a string matches a regex pattern.

Haskell (<> <*> <$> >>= >=> :: $ .)

There's much to explain with Haskell, as it's quite unique. What I find most interesting are these three: the double colon (::) which checks/assigns type signatures, the dollar ($) which enables you to chain operations without parentheses, and the dot (.) which is function composition.

Julia (' \ .+ <: : ===)

Julia has what appears to be a tranpose operator (') but this is actually for complex conjugate (so close!). There is left divide (\) which conveniently solves linear algebra equations where multiplicative order matters (Ax = b becomes x = A\b). The dot (.) is the broadcasting operator which makes certain operations elementwise ([1,2,3] .+ [3,4,5] == [4,6,8]). The subtype operator (<:) checks whether a type is a subtype or a class is a subclass (Dog <: Animal). Julia has ranges built into the syntax, so colon (:) creates an inclusive range (1:5 == [1,2,3,4,5]). Lastly, the triple equals (===) checks object identity, and is semantic sugar for Python's "is".

APL ( ∘.× +/ +\ ! )

APL features reductions (+/) and scans (+\) as core operations. For a given list A = [1,2,3,4], you could write +/A == 1+2+3+4 == 10 to perform a sum reduction. The beauty of this is it can apply to any operator, so you can do a product, for all (reduce on AND), there exists/any (reduce on OR), all equals and many more! There's also the inner and outer product (A+.×B A∘.×B)—the first gets the matrix product of A and B (by multiplying then summing result elementwise), and second gets a cartesian multiplication of each element of A to each of B (in Python: [a*b for a in A for b in B]). APL has a built-in operator for factorial and n-choose-k (!) based on whether it's unary or binary. APL has many more fantastic operators but it would be too much to list here. Have a look for yourself! https://en.wikipedia.org/wiki/APL_syntax_and_symbols

Others (:=: ~> |>)

Icon has an exchange operator (:=:) which obviates the need for a temp variable (a :=: b akin to Python's (a,b) = (b,a)). Scala has the category type operator (~>) which specifies what each type maps to/morphism ((f: Mapping[B, C]) === (f: B ~> C)). Lastly there's the infamous pipe operator (|>) popular for chaining methods together in functional languages like Elixir. R has the same concept denoted with (%>%).

It would be nice to have a language that featured many of these all at the same time. Of course, tradeoffs are necessary when devising a language; not everyone can be happy. But methinks we're failing as language designers.

By no means comprehensive, the link below collates the operators of many languages all into the same place, and makes a great reference guide:

https://rosettacode.org/wiki/Operator_precedence

Operators I wish were available:

  1. Root/Square Root

  2. Reversal (as opposed to Python's [::-1])

  3. Divisible (instead of n % m == 0)

  4. Appending/List Operators (instead of methods)

  5. Lambda/Mapping/Filters (as alternatives to list comprehension)

  6. Reduction/Scans (for sums, etc. like APL)

  7. Length (like Lua's #)

  8. Dot Product and/or Matrix Multiplication (like @)

  9. String-specific operators (concatentation, split, etc.)

  10. Function definition operator (instead of fun/function keywords)

  11. Element of/Subset of (like ∈ and ⊆)

  12. Function Composition (like math: (f ∘ g)(x))

What are your favorite operators in languages or operators you wish were included?

171 Upvotes

243 comments sorted by

View all comments

Show parent comments

1

u/frenris Oct 23 '22 edited Oct 23 '22

big fan of bitwise xor. unlike exponentiation it actually usually corresponds to an operation at the instruction set level. as such think it makes more sense as a primitive. xor is useful whenever you might want to toggle a value, though frequently you want a logical xor which many languages do not support.

||= is distinct from //= ; or equals versus defined or equals. 0 || 1 returns 1 ; 0 // 1 returns 0 -- undef // 1 returns 1.

Initialize and populate I think is addressed adequately by such conditional sets, assigns ;


  a \\\\= []

  a += b 

I think is preferable to some initialize + increment operators.

I'm not a fan of how perl does list length / last index. Think .end and .length methods would be cleanest.

Mathematical style ranges - (), [), (], [] are very appealing for their conciseness, but I expect would be impossible to parse in the presence of nesting. Think the need for them is mostly obviated if you have a convenient way to refer to list sizes versus final indexes.

Personally think ranges with a "to" keyword is nicest. [0 to 5] I think is very readable.

++ seems sensible for concatenation. Much better use of the symbol than increment -- thought it could confuse some users.

I've liked the idea of {+} for concatenation, {*} for replication -- but that's primarily because verilog uses {} for concatenation, replication -- e.g. {"a","b"} becomes "ab"; {3{"a"}} becomes "aaa". If one used ++ for concatenation instead though, I wonder what operator becomes natural to use for replication?

  • **

  • ++*

  • +*

I think ideally too you would have a suffix / prefix removal operator which would mirror this format.

E.g.


   a = "foo" {-} "oo" # a is "f"

   b = {-} "f" {+} "foo" {-} "o" # b is "o"

   c = "foo" {-} "bar" # throws exception or returns error type

One could then similarly define similar prefix / postfix relations


  "f" {<} "foo" # returns true

  "o" {<} "foo" # returns false

  "foo" {>} "o" returns true

  "foo" {>} "f" returns false

This is where I think operators make sense - rather than just abbreviating code you have a group of similar operations which together form a sort of coherent algebra, behave the same on different types of objects (lists, strings)

Similarly inner, exterior products of lists is another area where I think novel operators might be justified.

0

u/Uploft ⌘ Noda Oct 23 '22

Mathematical style ranges - (), [), (], [] are very appealing for their conciseness, but I expect would be impossible to parse in the presence of nesting.

It depends on how you do indexing. I can't remember if this is true for APL, but I made it idiomatically identical to write lst1[*lst2] as lst1[lst2] so the nesting problem is usually averted. I've considered having ranges be discontinuous, such that [0,2,5,7:9,13:) could be a range of its own, and the same thinking applies. Overall the nesting problem has not arisen.

I'm averse to infix keywords wherever possible. I want the user to see infix (and prefix) word-operators as almost entirely user-defined behavior, so it stands out amongst the symbols.

I currently use ++ for list (and string) concatentation, but I also stole from Julia in using * for string concatenation and ^ for string repetition (there's the power again). There's a couple reasons for this—for one, repetition makes way more sense as a power: "the"^3 == "thethethe". Maybe that's just me.

Of course, you might be wondering why I even want to appropriate arithmetic for string operations, and the reason being is I work with lists of strings a lot. And I'm designing an array-oriented language (where arithmetic goes elementwise, but set and list operations still operate on an object-level). So when I combine lists of strings like ["s","qu"] ++ ["at","it"] == ["s","qu","at","it"] but ["s","qu"] * ["at","it"] == ["sat","quit"] it's extra convenient. And when things get really nested, it's easier to read, because I know ++ will generally be referring to list/set operations whilst * will be referring to string combinations. In turn, I worry less about nesting bugs.

Partly related, since * and ^ are used for string concatenation and repetition, + is seized for space concatenation: "do"+"it"=="do it" which is more convenient than you may realize. Plus, if you have a list of words, sum(words) returns a space-delimited sentence!

To your previous question, ** would be more natural for replication (assuming ++ is used on strings), but I use that for intersection instead.

I'm generally not a fan of {°} operations. These are fine as constructs, like scans or reductions, but as an operator in an of itself it feels hacky to me. Nonetheless I'm intrigued and I will try to experiment with it. Maybe [++] could be a clever way of doing nested list concatenation? The visuals are there.