Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Spoiled by Perl

by holli (Abbot)
on Oct 31, 2021 at 20:58 UTC ( #11138279=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11138279]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-11-28 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?