http://www.perlmonks.org?node_id=11138279


in reply to Spoiled by Perl

I know the feeling. It's how people who learned Raku look at Perl: A good idea with sugar added later, but still leaves a lot to be desired.

In Raku, if you were to sort on the result of a method you would just say
@somelist.sort( *.Foo );
This will even use a schwartzian transform if the optimizer decides it is worth it.


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: Spoiled by Perl (sorting sugar)
by LanX (Sage) on Oct 31, 2021 at 21:46 UTC
    coincidentally I procrastinated this Sunday implementing syntactic sugar for exactly this in Perl5.

    There are some edge-cases tho, which keep we wondering ...

    In Raku:

    • how do you decide its a cmp or <=> ?
    • what if you want to apply another ordering? Like Czech sorting?
    • how do you sort by multiple criteria? Method chaining?

    > This will even use a schwartzian transform if the optimizer decides it is worth it.

    an optimizer detecting a good case for a Guttman-Rosler Transform transform would be more impressive tho. ;-)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      how do you decide its a cmp or <=> ?

      The dispatcher does that by finding the appropriate multi-sub that implements the actual comparison.

      # cmp. Generic, "smart" three-way comparator. multi sub infix:<cmp>(Any, Any) multi sub infix:<cmp>(Real:D, Real:D) multi sub infix:<cmp>(Str:D, Str:D) multi sub infix:<cmp>(Version:D, Version:D) # Compares strings with string semantics, numbers with number semantic +s, Pair objects first by key and then by value etc. # if $a eqv $b, then $a cmp $b always returns Order::Same. Otherwise O +rder::Less or Order::More.
      what if you want to apply another ordering? Like Czech sorting?
      Rakus Str class is fully Unicode aware and by default sorts characters on the numeric value of the codepoint.
      dd 'a ä b'.comb.sort; (" ", " ", "a", "b", "ä").Seq;
      If you wanted to implement your own behaviour you could just add another multi
      subset CzechString of String; multi sub infix:<cmp>(CzechString $a, CzechString $b) { # sort logic here }
      Or you can simply pass your comparator to sort
      sub czech-sort(String $a, String $b) { # sort logic here } @czech-words.sort( &czech-sort );
      how do you sort by multiple criteria? Method chaining?
      You can adress that in the comparator like in Perl, or make the comparator return a list of things.
      dd ([1,'z'], [2,'a'], ['1', 'a']).sort( *.[0,1] ); (["1", "a"], [1, "z"], [2, "a"]).Seq dd ([1,'z'], [2,'a'], ['1', 'a']).sort( { $^a[0] <=> $^b[0] || $^a[1] +cmp $^b[1] });


      holli

      You can lead your users to water, but alas, you cannot drown them.
        thanks! :)

        > The dispatcher does that by finding the appropriate multi-sub that implements the actual comparison.

        ehm ... if I have an array with numbers and strings it will implicitly decide based on the type of $a and $b which comparison to apply?

        > Rakus Str class is fully Unicode aware and by default sorts characters on the numeric value of the codepoint.

        I'm not sure if that's sufficient to handle different standards.

        E.g. the telephone book in Germany has another sorting than the dictionaries.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        update

        clearer rewording

Re^2: Spoiled by Perl
by Jenda (Abbot) on Dec 07, 2021 at 09:37 UTC
    var sorted = somelist.OrderBy(x => x.Foo());

    But yeah, the woke butterfly that killed Perl wins on character count. The twenty years of waiting "paid off".

    Jenda
    1984 was supposed to be a warning,
    not a manual!

      The twenty years of waiting "paid off".
      Yes. Indeed it has. Check out this Raku code of mine which is a solution for this years day 14. Any experienced programmer should be able to make sense of, and mostly understand it after spending 2 hours with the docs. I doubt the same is true for any perl implementation of similar brevity.
      my ( \o, \r ) = .head.comb.List, .tail.comb( /\w/ ).map( -> $a, $b, $c { "$a$b" => [ "$a$c", "$c$b" + ] } ).Hash with cache $*IN.slurp.split: "\n\n"; sub day14( \n ) { my %d = o.rotor( 2 => -1 )».join.Bag; %d = [(+)] %d.map({ r.{.key} »=>» .value }) for ^n; .Bag.values.minmax.elems - 1 with ( o.tail => 1, |%d ).map: { .key.comb[0] => .value }; } say day14 10|40;


      holli

      You can lead your users to water, but alas, you cannot drown them.

        Indeed it has, if the task was to destroy Perl.

        Also ... in an attempt to improve Perl's reputation of write-only line-noise language we ended up with this. Something somewhere along the way went terribly wrong. What makes you think an experienced programmer would want to waste two hours of his time with the docs? A "cute" infantile butterfly?

        Jenda
        1984 was supposed to be a warning,
        not a manual!

      Nice. Complete version:

      var sorted = someList.OrderBy( _ => _.Foo ).ThenBy( _ => _.Bar );

      I think the above is produced by the following:

      var sorted = from item in someList orderby item.Foo, item.Bar select item;