in reply to A set of new operators. In keeping with the design of Perl?

We also have the ternary operator ? : ; which is often used in expressions like

That'll be  ?? :: in Perl6.

?>:=

I think that is too much information to store in 4 ASCII characters. Some might argue that ? : already goes too far with information-packing. I personally learned C/C++ in a high school class (one of the very few languages I actually took a structured class for) and was never taught about the ? : operator. I never saw it until I learned Java, and even then I almost never used it until I learned Perl. It was a shortcut (and one of the things Java avoids religiously is shortcuts), and I didn't really "get it" for quite some time. Naturally, Perl religiously embraces shortcuts, but I think the operators you propose go too far.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: A set of new operators. In keeping with the design of Perl?
by BrowserUk (Patriarch) on May 19, 2003 at 15:03 UTC

    Maybe... Would it still be if the operators used the names TheDamian suggested (for the following two cases)?

    $x =min $x, $y; # p5 $x ?>:= $y; # my half-assed notion:) $x min= $y; # p6+ $x =max $x, $y; # p5 $x ?<:= $y; # Mine $x max= $y; # p6+

    The most recent situation where I encountered this was

    for ( 0 .. $#{ $hash{$set}{data} } ) { $hash{$set}{min} = $hash{$set}{min} < $hash{$set}{data}[$_] ? $hash{$set}{min} : $hash{$set}{data}[$_]; }

    which starkly demonstrates the problem. Contrast that with

    $hash{$set}{min} ?<:= $hash{$set}{data}[$_] for 0 .. $#{ $hash{$set}{d +ata} };

    Which is easier to read?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      for ( 0 .. $#{ $hash{$set}{data} } ) { $hash{$set}{min} = $hash{$set}{min} < $hash{$set}{data}[$_] ? $hash{$set}{min} : $hash{$set}{data}[$_]; }

      I personally find the above solution easier to read. However, since this is in a loop, and regular ? : evaluates the data twice, your proposed operator could be considerably faster in some cases. Naturally, the premature optimization alarms are ringing here, so I'm going to stop typing now (:

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

      $x =min $x, $y; # p5
      For me that's perfect. A function min - thats just three chars, you add = and what you have is the same number of chars (for golfers), much more clear than ?<:=, and more flexible - it can handle lists instead of just pairs.
      Update: Sorry this was just just a temporary blurr of my consiousness. It did not address the arguments by BrowserUk.

      I think you should at least consider writing such code more like:

      my $set = $hash{$set}; my $data = $set->{data}; for (@$data) { $set->{min} = $set->{min} < $_ ? $set->{min} : $_; }

      Hugo
      use List::Util qw(min); $hash{$set}{min} = min @{$hash{$set}{data}};
      I love that module. Thank goodness it's in the core since 5.8. Of course that doesn't address your beef, but in this case I think you're looking for aliasing, not a condensed form of the ternary. Some of that is available with Perl5 already, had you phrased your loop differently:
      for (@{$hash{$set}{data}}) { $hash{$set}{min} = $hash{$set}{min} < $_ ? $hash{$set}{min} : $_; }
      And in Perl6 (I'm probably getting the syntax wrong) it'd be something like:
      my $min := $hash{$set}{min}; for(@{%hash{$set}{data}}) { $min = $min < $_ ? $min : $_ }

      Makeshifts last the longest.

        List::Utils is great and was one the reasons I got around to upgrading. It replaced about half the routines in my personal utils module. The problem is that I need the min and max of each dataset, and iterating each dataset twice isn't ideal with huge datasets.

        I agree with your reduction when the array is of a reasonable size, but each dataset is 786,432 elements, there are several datasets and the operation isn't a one-off, so building a list in the for loop is kind of expensive I think? Hence the choice to use the lazy evaluation of the range op in a for loop to index the elements. That problem goes away once we get lazy-evaluating in P6.

        It still irks me that I have to type the operands, to what is essentially a binary operation, twice each, but it seems I'm the only one who sees that as a problem, and I don't relish causing, let alone being involved in, a 3 year p5p flame fest which I was told above is what it took to get ||= implemented:(.

        It's the same "I know that everything is available in the cpu for the processing required" type ire that I feel about the need to do

        my $n; my ($div, $rem) = ( int($n/10), $n % 10 );

        as I pontificated on at A better mod (%) operator?. It's not a "performance thing",

         my ($div, %rem) = $n %% 10;

        just seems cleaner. That I know that both values are available in the registers after either / or % means that even hiding the double division within a sub or overloading % or whatever still doesn't satisfy my sense of "once and once only:)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller