Reddit operator wish list

Run Settings
LanguageRaku
Language Version
Run Command
# See https://www.reddit.com/r/ProgrammingLanguages/comments/ya87l1/what_operators_do_you_wish_programming_languages/ # 1. Root/Square Root sub infix:<√> (Int \nth where * >= 0, Real $_ where * >= 0) { (.&roots: nth)[0].re } say ⁴√81; # 3 sub prefix:<√> ($_) { .sqrt } say √81; # 9 # 2. Reversal (as opposed to Python's [::-1]) # "Standard" Raku uses different ops for strings vs numbers vs lists. # "Standard" Raku uses `~` for some string ops. (Cuz `~` looks like a piece of string.) # Maybe pick a suitable Unicode symbol with a tilde in it? sub postfix:<⭁> (Str $_) { .flip } say 'Canada! 🇨🇦'⭁; # 🇨🇦 !adanaC # Maybe a surrounding pair of them?: sub circumfix:<⭁ ⭁> (Str $_) { .flip } say ⭁ 'Canada! 🇨🇦' ⭁; # 🇨🇦 !adanaC # Whatever syntax is chosen, perhaps the key thing is you get the right result, unlike Python: #print("Canada! 🇨🇦"[::-1]) # 🇦🇨 !adanaC # Oops. How come reversing Canada's flag yields the flag of the Ascension Islands!?! # Another arbitrary and ugly symbol, but maybe one that clearly communicates what it's doing: multi postfix:<⭾> ($_) { .reverse } say (1..10)⭾; # (10 9 8 7 6 5 4 3 2 1) # 3. Divisible (instead of n % m == 0) # Raku has a built in is-divisible-by operator: `infix:<%%>` say 42 %% 7; # True # 4. Appending/List Operators (instead of methods) # Lots in standard Raku. I came back to this after ones below but have run out of steam for tonight. # 5. Lambda/Mapping/Filters (as alternatives to list comprehension) # Lots in standard Raku. # For example, hyper meta operators `«` and `»`. # These compute/apply an operator to leaves of data structures using parallel processing semantics: say [1, 2, [3, 4, [5, 6], 7], 8, [9]] «+» [9, 8, [7, 6, [5, 4], 3], 2, [1]]; # Displays: # [10 10 [10 10 [10 10] 10] 10 [10]] # Of course, operands can have different lengths, shapes, etc. # And there's the scalar vs array nature of individual ops. # There are neat solutions to this but I'll defer further details. # 6. Reduction/Scans (for sums, etc. like APL) # Raku uses a `[op]` prefix for that: say [+] 1..100; # 5050 say sum 1..100; # 5050 # 7. Length (like Lua's #) # Raku lets users overload most characters but reserves `#` for end-of-line comments. # Does one pick a symbol based on what it looks like?: sub prefix:<⟷> (Str $_) { .chars } say ⟷'13 characters'; # 13 # Or try to stick with ASCII? sub postfix:<*> (Str $_) { .chars } say '13 characters'*; # 13 # 8. Dot Product # Raku has a `Z` infix metaoperator that "zips" two operands: say [+] (1, 2, 3) Z* (4, 5, 6); # 32 # 8. Matrix Multiplication # Stealing from https://rosettacode.org/wiki/Matrix_multiplication#Raku # :~:text=version%2C%20expressing%20the-,product%20of%20two%20matrices,-as%20the%20cross sub infix:<×>(@A, @B) { cross(@A, ([Z] @B), with => { [+] @^a Z* @^b }).rotor(@B) } .say for [[1, 1, 1, 1], [2, 4, 8, 16], [3, 9, 27, 81], [4, 16, 64, 256]] × [[ 4 , -3 , 4/3, -1/4 ], [-13/3, 19/4, -7/3, 11/24], [ 3/2, -2 , 7/6, -1/4 ], [ -1/6, 1/4, -1/6, 1/24]]; # Displays: # # [1 0 0 0] # [0 1 0 0] # [0 0 1 0] # [0 0 0 1] # 9. String-specific operators (concatentation, split, etc.) # I'm getting very tired but really want to post this tonight. say 'foo' ~ 'bar'; # foobar # The world's richest regex/grammar language too, with a bazillion operators. # 10. Function definition operator (instead of fun/function keywords) say (1..10) .map: *³; # (1 8 27 64 125 216 343 512 729 1000) # Combining the `*` character as an operand with an operator creates a lambda. # A superscript number as a postfix op raises its operand to that number as a power. # Raku has a rich range of ways to express functions. # 11. Element of/Subset of (like ∈ and ⊆) # `∈` and `⊆` are in standard Raku # See https://docs.raku.org/routine/(elem),%20infix%20%E2%88%88 # 12. Function Composition (like math: (f ∘ g)(x)) # `∘` (alias `o`, just the lowercase ASCII letter) is in standard Raku # See https://rosettacode.org/wiki/Function_composition#Raku
Editor Settings
Theme
Key bindings
Full width
Lines