Mailing List Archive

Feature request / RFC: builtin for numeric sort
Every Perl programmer has hundreds of times written

sort { $a <=> $b } @stuff

to get a numeric sort. Sometimes it takes an outsider to spot things, and I found myself explaining to another programmer that Perl doesn't have a builtin syntax for sorting numerically, but this incantation with its magic $a and $b variables is the way it's always been done.

I propose a new builtin

sortnum @stuff

exactly equivalent to the above, but a bit less baroque, and it might likely be a touch faster.

Benefits: easy things should be easy. Sorting by numeric value is such a common operation that it deserves its own keyword. Generally, Perl provides two versions of each builtin (so we have both == and eq, both <= and le, etc) but there is not a numeric equivalent of 'sort' without an additional code block.

Problems: if the word sortnum is already used in someone's program. It could be protected by 'use version'.

This is a subjective opinion, of course I understand that you can write sort {$a<=>$b}, you can package it as a subroutine, you can provide it as a CPAN module... but the request is specifically that it's common enough to be a builtin. Despite twenty years of muscle memory writing it out, I think Perl would be a better language if it had numeric sort out of the box.

--
Ed Avis <ed.avis@qmaw.com>
Please ignore confidentiality gunge below this point.

This email and any files transmitted with it are CONFIDENTIAL and are intended solely for the use of the individual(s) or entity to whom they are addressed. Any unauthorised copying, disclosure or distribution of the material within this email is strictly forbidden. Any views or opinions presented within this email are solely those of the author and do not necessarily represent those of PGIM Limited, QMA Wadhwani LLP or their affiliates unless otherwise specifically stated. An electronic message is not binding on its sender. Any message referring to a binding agreement must be confirmed in writing and duly signed. If you have received this email in error, please notify the sender immediately and delete the original. Telephone, electronic and other communications and conversations with PGIM Limited, QMA Wadhwani LLP and/or their associated persons may be recorded and retained. PGIM Limited and QMA Wadhwani LLP are authorised and regulated by the Financial Conduct Authority. PGIM Limited (registered in England No. 3809039) has its registered office at Grand Buildings, 1-3 Strand, Trafalgar Square, London WC2N 5HR and QMA Wadhwani LLP (registered in England No. OC303168) has its registered office at 9th Floor, Orion House, 5 Upper St. Martin's Lane, London, England, WC2H 9EA.

Please note that your personal information may be stored and processed in any country where we have facilities or in which we engage service providers. If you provide personal information to us by email or otherwise, you consent to the transfer of that information to countries outside of your country of residence and these countries may have different data protection rules than your country.

To learn about our privacy policies, please use this link<https://www.pgim.com/disclaimer/privacy-center> to read the Privacy Notices.
Feature request / RFC: builtin for numeric sort [ In reply to ]
Every Perl programmer has hundreds of times written

sort { $a <=> $b } @stuff

to get a numeric sort. Sometimes it takes an outsider to spot things, and I found myself explaining to another programmer that Perl doesn't have a builtin syntax for sorting numerically, but this incantation with its magic $a and $b variables is the way it's always been done.

I propose a new builtin

sortnum @stuff

exactly equivalent to the above, but a bit less baroque, and it might likely be a touch faster.

Benefits: easy things should be easy. Sorting by numeric value is such a common operation that it deserves its own keyword. Generally, Perl provides two versions of each builtin (so we have both == and eq, both <= and le, etc) but there is not a numeric equivalent of 'sort' without an additional code block.

Problems: if the word sortnum is already used in someone's program. It could be protected by 'use version'.

This is a subjective opinion, of course I understand that you can write sort {$a<=>$b}, you can package it as a subroutine, you can provide it as a CPAN module... but the request is specifically that it's common enough to be a builtin. Despite twenty years of muscle memory writing it out, I think Perl would be a better language if it had numeric sort out of the box.

--
Ed Avis <ed.avis@qmaw.com>

This email and any files transmitted with it are CONFIDENTIAL and are intended solely for the use of the individual(s) or entity to whom they are addressed. Any unauthorised copying, disclosure or distribution of the material within this email is strictly forbidden. Any views or opinions presented within this email are solely those of the author and do not necessarily represent those of PGIM Limited, QMA Wadhwani LLP or their affiliates unless otherwise specifically stated. An electronic message is not binding on its sender. Any message referring to a binding agreement must be confirmed in writing and duly signed. If you have received this email in error, please notify the sender immediately and delete the original. Telephone, electronic and other communications and conversations with PGIM Limited, QMA Wadhwani LLP and/or their associated persons may be recorded and retained. PGIM Limited and QMA Wadhwani LLP are authorised and regulated by the Financial Conduct Authority. PGIM Limited (registered in England No. 3809039) has its registered office at Grand Buildings, 1-3 Strand, Trafalgar Square, London WC2N 5HR and QMA Wadhwani LLP (registered in England No. OC303168) has its registered office at 9th Floor, Orion House, 5 Upper St. Martin's Lane, London, England, WC2H 9EA.

Please note that your personal information may be stored and processed in any country where we have facilities or in which we engage service providers. If you provide personal information to us by email or otherwise, you consent to the transfer of that information to countries outside of your country of residence and these countries may have different data protection rules than your country.

To learn about our privacy policies, please use this link<https://www.pgim.com/disclaimer/privacy-center> to read the Privacy Notices.
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
On Thu, 1 Jul 2021 07:04:03 +0000, Ed Avis <ed.avis@qmaw.com> wrote:

> Every Perl programmer has hundreds of times written
>
> sort { $a <=> $b } @stuff
>
> to get a numeric sort. Sometimes it takes an outsider to spot
> things, and I found myself explaining to another programmer that Perl
> doesn't have a builtin syntax for sorting numerically, but this
> incantation with its magic $a and $b variables is the way it's always
> been done.
>
> I propose a new builtin
>
> sortnum @stuff

I have no objections in adding this to List::Util, which is Dual-Life

> exactly equivalent to the above, but a bit less baroque, and it might
> likely be a touch faster.
>
> Benefits: easy things should be easy. Sorting by numeric value is
> such a common operation that it deserves its own keyword. Generally,
> Perl provides two versions of each builtin (so we have both == and
> eq, both <= and le, etc) but there is not a numeric equivalent of
> 'sort' without an additional code block.
>
> Problems: if the word sortnum is already used in someone's program.
> It could be protected by 'use version'.
>
> This is a subjective opinion, of course I understand that you can
> write sort {$a<=>$b}, you can package it as a subroutine, you can
> provide it as a CPAN module... but the request is specifically that
> it's common enough to be a builtin. Despite twenty years of muscle
> memory writing it out, I think Perl would be a better language if it
> had numeric sort out of the box.

--
H.Merijn Brand https://tux.nl Perl Monger http://amsterdam.pm.org/
using perl5.00307 .. 5.33 porting perl5 on HP-UX, AIX, and Linux
https://tux.nl/email.html http://qa.perl.org https://www.test-smoke.org
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
??????? Original Message ???????

On Thursday, July 1st, 2021 at 2:10 AM, H.Merijn Brand <perl5@tux.freedom.nl> wrote:

> On Thu, 1 Jul 2021 07:04:03 +0000, Ed Avis ed.avis@qmaw.com wrote:
>
> > Every Perl programmer has hundreds of times written
> >
> > sort { $a <=> $b } @stuff
> >
> >
> > to get a numeric sort. Sometimes it takes an outsider to spot
> >
> > things, and I found myself explaining to another programmer that Perl
> >
> > doesn't have a builtin syntax for sorting numerically, but this
> >
> > incantation with its magic $a and $b variables is the way it's always
> >
> > been done.
> >
> > I propose a new builtin
> >
> > sortnum @stuff
> >
>
> I have no objections in adding this to List::Util, which is Dual-Life

++

>
> > exactly equivalent to the above, but a bit less baroque, and it might
> >

This is the problem with things like trim(), etc - suddently there is a million "keywords" that are really shorthand for things people can't be bothered to learn the local idioms or actual details. This is why people bring up PHP all the time - there is no moderation when it comes to this sort of thing and keyword/namespace pollution. No thanks.

> > likely be a touch faster.

I am interested in learning how - can you explain what you mean here? Not saying it can't be faster, just don't really see past the hand waiving here.

> >
> > Benefits: easy things should be easy. Sorting by numeric value is
> >
> > such a common operation that it deserves its own keyword. Generally,
> >
> > Perl provides two versions of each builtin (so we have both == and
> >
> > eq, both <= and le, etc) but there is not a numeric equivalent of
> >
> > 'sort' without an additional code block.
> >
> > Problems: if the word sortnum is already used in someone's program.
> >
> > It could be protected by 'use version'.
> >
> > This is a subjective opinion, of course I understand that you can
> >
> > write sort {$a<=>$b}, you can package it as a subroutine, you can
> >
> > provide it as a CPAN module... but the request is specifically that
> >
> > it's common enough to be a builtin. Despite twenty years of muscle
> >
> > memory writing it out, I think Perl would be a better language if it
> >
> > had numeric sort out of the box.

You mean unlike sort(1), which sorts lexicographically "out of the box"?

Most of Perl/perl "default" things can be understood better if you understand how much the *nix userland has influenced it.

Brett

>
> H.Merijn Brand https://tux.nl Perl Monger http://amsterdam.pm.org/
>
> using perl5.00307 .. 5.33 porting perl5 on HP-UX, AIX, and Linux
>
> https://tux.nl/email.html http://qa.perl.org https://www.test-smoke.org
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
On Thu, Jul 01, 2021 at 07:04:03AM +0000, Ed Avis wrote:
> Every Perl programmer has hundreds of times written
>
> sort { $a <=> $b } @stuff
>
> to get a numeric sort. Sometimes it takes an outsider to spot things, and I found myself explaining to another programmer that Perl doesn't have a builtin syntax for sorting numerically, but this incantation with its magic $a and $b variables is the way it's always been done.
>
> I propose a new builtin
>
> sortnum @stuff
>
> exactly equivalent to the above, but a bit less baroque, and it might likely be a touch faster.

It's unlikely to be faster. `sort { $a <=> $b }` is optimised to C.
Note that the entire sort block has gone:

$ perl -MO=Concise -e 'sort { $a <=> $b } @stuff'
7 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
6 <@> sort vK/NUM ->7
3 <0> pushmark s ->4
5 <1> rv2av[t5] lK/1 ->6
4 <#> gv[*stuff] s ->5
-e syntax OK

Compare with

$ perl -MO=Concise -e 'sort { $a <=>( $b + 0) } @stuff'
7 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
6 <@> sort vKS* ->7
3 <0> pushmark s ->4
- <1> null sK/1 ->-
- <@> scope sK ->(end)
- <;> ex-nextstate(main 2 -e:1) v ->-
- <2> ncmp[t4] sK/2 ->(end)
- <1> ex-rv2sv sK/1 ->-
- <#> gvsv[*a] s ->-
- <2> add[t3] sKP/2 ->-
- <1> ex-rv2sv sK/1 ->-
- <#> gvsv[*b] s ->-
- <$> const[IV 0] s ->-
5 <1> rv2av[t6] lKM/1 ->6
4 <#> gv[*stuff] s ->5
-e syntax OK

where all of the sort block remains.


The descending sort is also optimised:

$ perl -MO=Concise -e 'sort { $b <=> $a } @stuff'
7 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
6 <@> sort vK/DESC,NUM ->7
3 <0> pushmark s ->4
5 <1> rv2av[t5] lK/1 ->6
4 <#> gv[*stuff] s ->5
-e syntax OK


This is something your proposed sortnum can't do.


`reverse sort { $a <=> $b}` is also optimised, but that can't be demonstrated
in void context, so:

$ perl -MO=Concise -e '@stuff = reverse sort {$a <=> $b} @stuff'
c <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
b <2> aassign[t8] vKS/COM_AGG ->c
- <1> ex-list lK ->8
3 <0> pushmark s ->4
- <1> ex-reverse lK/1 ->-
4 <0> pushmark s ->5
7 <@> sort lK/REV,NUM ->8
- <0> ex-pushmark s ->5
6 <1> rv2av[t7] lK/1 ->7
5 <#> gv[*stuff] s ->6
- <1> ex-list lK ->b
8 <0> pushmark s ->9
a <1> rv2av[t2] lKRM*/1 ->b
9 <#> gv[*stuff] s ->a
-e syntax OK


Again, all the $a and $b is gone from the optree.

This is an optimisation your proposed sortnum *could* have, if implemented
as a builtin. But any "backport" implementation on CPAN couldn't do this,
as it relies on C code in the peephole optimiser.


It will not be faster.

Nicholas Clark
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
I thought that numeric sort implemented as a builtin, in C code, would be faster than

sort { $a <=> $b }

where the comparison function has to run a piece of Perl code, which although well optimized (I know that $a and $b are magic global variables for performance) is still never going to beat C for seeing which of two numbers is greater.

However, as Nicholas Clark pointed out, the longer syntax is already recognized and special-cased to a native numeric sort. It doesn't have to do a symbol table lookup of $a and $b on each comparison. So there already is a builtin, it's just not immediately obvious you are using it.

'reverse sortnum' could, of course, be optimized to a native reverse sort just as 'reverse sort { $a <=> $b }' currently is.


This email and any files transmitted with it are CONFIDENTIAL and are intended solely for the use of the individual(s) or entity to whom they are addressed. Any unauthorised copying, disclosure or distribution of the material within this email is strictly forbidden. Any views or opinions presented within this email are solely those of the author and do not necessarily represent those of PGIM Limited, QMA Wadhwani LLP or their affiliates unless otherwise specifically stated. An electronic message is not binding on its sender. Any message referring to a binding agreement must be confirmed in writing and duly signed. If you have received this email in error, please notify the sender immediately and delete the original. Telephone, electronic and other communications and conversations with PGIM Limited, QMA Wadhwani LLP and/or their associated persons may be recorded and retained. PGIM Limited and QMA Wadhwani LLP are authorised and regulated by the Financial Conduct Authority. PGIM Limited (registered in England No. 3809039) has its registered office at Grand Buildings, 1-3 Strand, Trafalgar Square, London WC2N 5HR and QMA Wadhwani LLP (registered in England No. OC303168) has its registered office at 9th Floor, Orion House, 5 Upper St. Martin's Lane, London, England, WC2H 9EA.

Please note that your personal information may be stored and processed in any country where we have facilities or in which we engage service providers. If you provide personal information to us by email or otherwise, you consent to the transfer of that information to countries outside of your country of residence and these countries may have different data protection rules than your country.

To learn about our privacy policies, please use this link<https://www.pgim.com/disclaimer/privacy-center> to read the Privacy Notices.
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
On Thu, Jul 1, 2021 at 2:51 PM Ed Avis <ed.avis@qmaw.com> wrote:

> Every Perl programmer has hundreds of times written
>
> sort { $a <=> $b } @stuff
>
> to get a numeric sort. Sometimes it takes an outsider to spot things, and
> I found myself explaining to another programmer that Perl doesn't have a
> builtin syntax for sorting numerically, but this incantation with its magic
> $a and $b variables is the way it's always been done.
>
> I propose a new builtin
>
> sortnum @stuff
>
> exactly equivalent to the above, but a bit less baroque, and it might
> likely be a touch faster.
>
> Benefits: easy things should be easy. Sorting by numeric value is such a
> common operation that it deserves its own keyword. Generally, Perl
> provides two versions of each builtin (so we have both == and eq, both <=
> and le, etc) but there is not a numeric equivalent of 'sort' without an
> additional code block.
>
> Problems: if the word sortnum is already used in someone's program. It
> could be protected by 'use version'.
>
> This is a subjective opinion, of course I understand that you can write
> sort {$a<=>$b}, you can package it as a subroutine, you can provide it as a
> CPAN module... but the request is specifically that it's common enough to
> be a builtin. Despite twenty years of muscle memory writing it out, I
> think Perl would be a better language if it had numeric sort out of the box.
>

This seems to be a duplicate post?

-Dan
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
2021-7-1 20:23 Ed Avis <ed.avis@qmaw.com> wrote:

> I thought that numeric sort implemented as a builtin, in C code, would be
> faster than
>
> sort { $a <=> $b }
>
> where the comparison function has to run a piece of Perl code, which
> although well optimized (I know that $a and $b are magic global variables
> for performance) is still never going to beat C for seeing which of two
> numbers is greater.
>
> However, as Nicholas Clark pointed out, the longer syntax is already
> recognized and special-cased to a native numeric sort. It doesn't have to
> do a symbol table lookup of $a and $b on each comparison. So there already
> is a builtin, it's just not immediately obvious you are using it.
>
> 'reverse sortnum' could, of course, be optimized to a native reverse sort
> just as 'reverse sort { $a <=> $b }' currently is.
>
>
Thank you for posting ideas.

If the essence of the problem is google/bing/duckduckgo searchability, I
suggest the idea of preparing a web document to sort Perl numbers.

I personally have a desire to create Perl web documentation for beginners.
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
On Thu, Jul 1, 2021 at 4:37 PM Yuki Kimoto <kimoto.yuki@gmail.com> wrote:

> Thank you for posting ideas.
>
> If the essence of the problem is google/bing/duckduckgo searchability, I
> suggest the idea of preparing a web document to sort Perl numbers.
>
> I personally have a desire to create Perl web documentation for beginners.
>

perlfaq (which is dual-life - "perldoc perlfaq") would be ideal to capture
these recipes and idioms.
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
On 7/3/21 12:07 PM, Karen Etheridge wrote:
>
> On Thu, Jul 1, 2021 at 4:37 PM Yuki Kimoto <kimoto.yuki@gmail.com
> <mailto:kimoto.yuki@gmail.com>> wrote:
>
> Thank you for posting ideas.
>
> If the essence of the problem is google/bing/duckduckgo
> searchability, I suggest the idea of preparing a web document to
> sort Perl numbers.
>
> I personally have a desire to create Perl web documentation for
> beginners.
>
>
> perlfaq (which is dual-life - "perldoc perlfaq") would be ideal to
> capture these recipes and idioms.
>

Neil is creating a perl quirks document.

The $a,$b is definitely a quirk which increases the cognitive load on
learning and programming Perl
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
Op 01-07-2021 om 09:04 schreef Ed Avis:
> Every Perl programmer has hundreds of times written
>
> sort { $a <=> $b } @stuff
>
> to get a numeric sort. Sometimes it takes an outsider to spot things, and I found myself explaining to another programmer that Perl doesn't have a builtin syntax for sorting numerically, but this incantation with its magic $a and $b variables is the way it's always been done.
>
> I propose a new builtin
>
> sortnum @stuff
>
> exactly equivalent to the above, but a bit less baroque, and it might likely be a touch faster.
>
> Benefits: easy things should be easy. Sorting by numeric value is such a common operation that it deserves its own keyword. Generally, Perl provides two versions of each builtin (so we have both == and eq, both <= and le, etc) but there is not a numeric equivalent of 'sort' without an additional code block.
>
> Problems: if the word sortnum is already used in someone's program. It could be protected by 'use version'.
>
> This is a subjective opinion, of course I understand that you can write sort {$a<=>$b}, you can package it as a subroutine, you can provide it as a CPAN module... but the request is specifically that it's common enough to be a builtin. Despite twenty years of muscle memory writing it out, I think Perl would be a better language if it had numeric sort out of the box.


As a user, I do support this.


Although I understand the concerns of poluting the global namespace,
this is so common that I think it warrants its own keyword. If I had to
choose between trim being a new keyword and this, I would choose this.
Trim has no comparable operation in core, whereas lexicographical sort
does exist in core. So in fact that is my position, trim in a dual life
module, num sort in core.

Actually, I would say 'sortn', but sortnum is fine too.


HTH,

M4
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
On Sun, 4 Jul 2021 12:03:35 +0200, Martijn Lievaart <m@rtij.nl> wrote:

> Op 01-07-2021 om 09:04 schreef Ed Avis:
> > Every Perl programmer has hundreds of times written
> >
> > sort { $a <=> $b } @stuff
> >
> > to get a numeric sort. Sometimes it takes an outsider to spot
> > things, and I found myself explaining to another programmer that
> > Perl doesn't have a builtin syntax for sorting numerically, but
> > this incantation with its magic $a and $b variables is the way it's
> > always been done.
> >
> > I propose a new builtin
> >
> > sortnum @stuff
> >
> > exactly equivalent to the above, but a bit less baroque, and it
> > might likely be a touch faster.
> >
> > Benefits: easy things should be easy. Sorting by numeric value is
> > such a common operation that it deserves its own keyword.
> > Generally, Perl provides two versions of each builtin (so we have
> > both == and eq, both <= and le, etc) but there is not a numeric
> > equivalent of 'sort' without an additional code block.
> >
> > Problems: if the word sortnum is already used in someone's program.
> > It could be protected by 'use version'.
> >
> > This is a subjective opinion, of course I understand that you can
> > write sort {$a<=>$b}, you can package it as a subroutine, you can
> > provide it as a CPAN module... but the request is specifically that
> > it's common enough to be a builtin. Despite twenty years of muscle
> > memory writing it out, I think Perl would be a better language if
> > it had numeric sort out of the box.
>
> As a user, I do support this.

As a user, *I* would not, as this is a slippery slope towards the
namespace pollution as we know from PHP: which of the 400+ string
functions do I require for a specific task.

I personally favor a function with *good documentation* that supports
the often-tasks intuitively and the seldom (and sometimes complicated)
tasks with well-names options. I know how hard the edge-cases are in
maint and support (think splice), but I would prefer new options to
sort than a new builtin name

sort :num @foo

is not yet an option, but it could be. And

$ perldoc -f sort

would tell me.

As a side note: with `sort { $a <=> $b } @foo` in my muscle-memory, it
is very easy to reverse it when needed to `sort { $b <=> $a } @foo` and
it would still be optimized. When I would have used a new function
sortnum or whatever, I would need to read the docs or add `reverse
sort` which would be a slowdown. And having a block (that might be
optimized) is also easy to change into a different or additional sort
order based on other criteria, which works very well in git history.

> Although I understand the concerns of poluting the global namespace,
> this is so common that I think it warrants its own keyword. If I had
> to choose between trim being a new keyword and this, I would choose
> this. Trim has no comparable operation in core, whereas
> lexicographical sort does exist in core. So in fact that is my
> position, trim in a dual life module, num sort in core.
>
> Actually, I would say 'sortn', but sortnum is fine too.
>
> M4

--
H.Merijn Brand https://tux.nl Perl Monger http://amsterdam.pm.org/
using perl5.00307 .. 5.33 porting perl5 on HP-UX, AIX, and Linux
https://tux.nl/email.html http://qa.perl.org https://www.test-smoke.org
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
Op 04-07-2021 om 12:35 schreef H.Merijn Brand:
> On Sun, 4 Jul 2021 12:03:35 +0200, Martijn Lievaart <m@rtij.nl> wrote:
>
>> Op 01-07-2021 om 09:04 schreef Ed Avis:
>>> Every Perl programmer has hundreds of times written
>>>
>>> sort { $a <=> $b } @stuff
>>>
>>> to get a numeric sort. Sometimes it takes an outsider to spot
>>> things, and I found myself explaining to another programmer that
>>> Perl doesn't have a builtin syntax for sorting numerically, but
>>> this incantation with its magic $a and $b variables is the way it's
>>> always been done.
>>>
>>> I propose a new builtin
>>>
>>> sortnum @stuff
>>>
>>> exactly equivalent to the above, but a bit less baroque, and it
>>> might likely be a touch faster.
>>>
>>> Benefits: easy things should be easy. Sorting by numeric value is
>>> such a common operation that it deserves its own keyword.
>>> Generally, Perl provides two versions of each builtin (so we have
>>> both == and eq, both <= and le, etc) but there is not a numeric
>>> equivalent of 'sort' without an additional code block.
>>>
>>> Problems: if the word sortnum is already used in someone's program.
>>> It could be protected by 'use version'.
>>>
>>> This is a subjective opinion, of course I understand that you can
>>> write sort {$a<=>$b}, you can package it as a subroutine, you can
>>> provide it as a CPAN module... but the request is specifically that
>>> it's common enough to be a builtin. Despite twenty years of muscle
>>> memory writing it out, I think Perl would be a better language if
>>> it had numeric sort out of the box.
>> As a user, I do support this.
> As a user, *I* would not, as this is a slippery slope towards the
> namespace pollution as we know from PHP: which of the 400+ string
> functions do I require for a specific task.


Which I addressed further down, which you did not snip, but also did not
answer.


>
> I personally favor a function with *good documentation* that supports
> the often-tasks intuitively and the seldom (and sometimes complicated)
> tasks with well-names options. I know how hard the edge-cases are in
> maint and support (think splice), but I would prefer new options to
> sort than a new builtin name
>
> sort :num @foo
>
> is not yet an option, but it could be. And
>
> $ perldoc -f sort
>
> would tell me.


That is a very good alternative, one I probably could get behind. It's
extendable and does not polute the global namespace. Probably a much
better proposal.

(It doesn't feel completely perlish to me, but I recognize that
':<attribute>' probably is the perl way of the future, and it is I who
has to change in this respect.)


> As a side note: with `sort { $a <=> $b } @foo` in my muscle-memory, it
> is very easy to reverse it when needed to `sort { $b <=> $a } @foo` and
> it would still be optimized. When I would have used a new function
> sortnum or whatever, I would need to read the docs or add `reverse
> sort` which would be a slowdown. And having a block (that might be
> optimized) is also easy to change into a different or additional sort
> order based on other criteria, which works very well in git history.


I find this argument hard to parse.

You reverse a lexicographical sort by using reverse, but you oppose
doing this for a numerical sort? With the argument that the current way
is in muscle memory? And because you have this in your muscle memory,
you would not be able to remember the numerical form and have to look it
up every time in the documentation? With that argument you can oppose
any change to the language.

(I'm not going into the slowdown, as it is not clear if you mean you
have to consult the docs, slowing you down, or that reverse sortnum
would slow down your program, but I think both arguments are bogus)

Finally, your argument that you would have to look it up, feels
completely weird when you just stated that '$perldoc -f sort' would tell
you how to use ':num' a few lines higher. Then this argument would apply
against that as well.


HTH,

M4
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
On Sun, Jul 04, 2021 at 12:35:55PM +0200, H.Merijn Brand wrote:

> As a user, *I* would not, as this is a slippery slope towards the
> namespace pollution as we know from PHP: which of the 400+ string
> functions do I require for a specific task.

I'm not sure if "slippery slope" is important to your reasoning here.

Having a lot of special-use functions (that are very good at the thing that
they do) *is* hard to remember.

Having simpler building blocks that can be composed into more complex things
is more flexible, and at scale, less things to learn and to remember.

> I personally favor a function with *good documentation* that supports
> the often-tasks intuitively and the seldom (and sometimes complicated)
> tasks with well-names options. I know how hard the edge-cases are in
> maint and support (think splice), but I would prefer new options to
> sort than a new builtin name
>
> sort :num @foo
>
> is not yet an option, but it could be. And

I was about to hit send with:

I *think* that this syntax is parseable.

("Everyone wants the colon.", "Larry gets the colon")

in that, many suggestions for colons "clash" with ternaries. But I don't
think that there's any way to write

$var ? ($long, $expression) = sort : ...

where that ':' currently makes sense as part of ? :


but, just in time, I tried it:

$ perl -wle 'sub num {return "Hi"}; @foo = $bar ? sort :num @baz; print @foo'
Name "main::bar" used only once: possible typo at -e line 1.
Name "main::baz" used only once: possible typo at -e line 1.
Hi


It's valid syntax. It would need to be feature gated.


> $ perldoc -f sort
>
> would tell me.


The thing that is still bugging me about

sort :num @foo


is that this, again, seems to be a one-off.

What *other* common (enough) operations are desired from sort such that
they'd get

sort :bar @foo;
sort :baz @foo;


because I don't think that we'd want to build a special syntax just for
one thing.

> As a side note: with `sort { $a <=> $b } @foo` in my muscle-memory, it
> is very easy to reverse it when needed to `sort { $b <=> $a } @foo` and
> it would still be optimized. When I would have used a new function
> sortnum or whatever, I would need to read the docs or add `reverse
> sort` which would be a slowdown. And having a block (that might be
> optimized) is also easy to change into a different or additional sort
> order based on other criteria, which works very well in git history.

Given that `reverse sort {$a <=> $b}` is optimised, there's no reason why
a *builtin* `sortnum` couldn't be similarly optimised.

But (no sane) way a backcompat CPAN pollyfill could be on existing perl
versions. Where `reverse sort {$a <=> $b}` is optimised.


Nicholas Clark
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
Ed Avis writes:
> Every Perl programmer has hundreds of times written
>
> sort { $a <=> $b } @stuff
>
> to get a numeric sort. Sometimes it takes an outsider to spot things,
> and I found myself explaining to another programmer that Perl doesn't
> have a builtin syntax for sorting numerically, but this incantation
> with its magic $a and $b variables is the way it's always been done.
>
> I propose a new builtin
>
> sortnum @stuff
>
> exactly equivalent to the above, but a bit less baroque, and it might
> likely be a touch faster.

Even if you just limit yourself to sorting (and the can of worms that is
string processing is already open for better or worse), following this
argument to the logical conclusion, you'll end up with dozens
specialized sort functions that each look like a useful and sensible
addition to language, but taken all together still make for a terribly
confusing result.

I tend to think that it might be possible to introduce "decorators" or
"traits" so that instead of the idiom above you'd be able to say
something like

sort {:num:} @stuff;

maybe even with stacking different decorations as in

sort {:num:pre-sorted:stable:in-place:} @stuff;

You get the drift (no, I'm not proposing this exact syntax or even the
feature, even though it should be unused). It wouldn't work quite as
well for builtin functions that don't accept anonymous subs to begin
with, although I think that a leading ":", omitting the braces, is
currently a syntax error in those places as well.

Decorators or traits are still confusing in that you need to know about
all the permissible combinations thereof, but it should clutter the
syntax table less. Note that if you start allowing users to define
their own traits or decorations you again end up with a namespace
problem (built-in vs. user-defined) that will look easy enough to deal
with but likely becomes increasingly knotty the longer this facility is
in use.


Regards,
--

Achim.
--
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

SD adaptations for KORG EX-800 and Poly-800MkII V0.9:
http://Synth.Stromeko.net/Downloads.html#KorgSDada
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
On Sun, Jul 04, 2021 at 09:04:39PM +0200, Achim Gratz wrote:
> Ed Avis writes:
> > Every Perl programmer has hundreds of times written
> >
> > sort { $a <=> $b } @stuff
> >
> > to get a numeric sort. Sometimes it takes an outsider to spot things,
> > and I found myself explaining to another programmer that Perl doesn't
> > have a builtin syntax for sorting numerically, but this incantation
> > with its magic $a and $b variables is the way it's always been done.
> >
> > I propose a new builtin
> >
> > sortnum @stuff
> >
> > exactly equivalent to the above, but a bit less baroque, and it might
> > likely be a touch faster.
>
> Even if you just limit yourself to sorting (and the can of worms that is
> string processing is already open for better or worse), following this
> argument to the logical conclusion, you'll end up with dozens
> specialized sort functions that each look like a useful and sensible
> addition to language, but taken all together still make for a terribly
> confusing result.

I keep thinking the same (at first) but then I can't usually see many more
than "ascending numeric" and "descending numeric".

> I tend to think that it might be possible to introduce "decorators" or
> "traits" so that instead of the idiom above you'd be able to say
> something like
>
> sort {:num:} @stuff;
>
> maybe even with stacking different decorations as in
>
> sort {:num:pre-sorted:stable:in-place:} @stuff;
>
> You get the drift (no, I'm not proposing this exact syntax or even the
> feature, even though it should be unused). It wouldn't work quite as
> well for builtin functions that don't accept anonymous subs to begin
> with, although I think that a leading ":", omitting the braces, is
> currently a syntax error in those places as well.

Sadly not. Anything that is a list operator that can accept zero arguments
can currently have a ':' immediately afterwards as part of a ternary:

perl -wle 'my ($bar, $baz); sub num {"Colon!"}; my $foo = $bar ? reverse :num $baz; print $foo'
Colon!

not that `reverse :num` makes much sense. But it's valid syntax, and as the
parser can't backtrack once it has committed to treating the input as a
particular syntax, it can't disambiguate between `:num` being a decorator,
and `:num` being the end of the ternary, no whitespace, and a function call.

But I think that the {: at the start of {:foo:bar:} is a syntax error
everywhere, even for builtins that don't take blocks.

> Decorators or traits are still confusing in that you need to know about
> all the permissible combinations thereof, but it should clutter the
> syntax table less. Note that if you start allowing users to define
> their own traits or decorations you again end up with a namespace
> problem (built-in vs. user-defined) that will look easy enough to deal
> with but likely becomes increasingly knotty the longer this facility is
> in use.

That's a useful observation.

Nicholas Clark
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
Den 2021-07-04 kl. 21:04 skrev Achim Gratz:
> I tend to think that it might be possible to introduce "decorators" or
> "traits" so that instead of the idiom above you'd be able to say
> something like
>
> sort {:num:} @stuff;
>
> maybe even with stacking different decorations as in
>
> sort {:num:pre-sorted:stable:in-place:} @stuff;

I would just do this:

sub num { $a <=> $b }
sort {num} @stuff;

Put num and some similar functions in a module, call it List::Sort.

Is there anything I'm missing?
Re: Feature request / RFC: builtin for numeric sort [ In reply to ]
On Sun, Jul 4, 2021 at 4:45 PM John Ankarström <john@ankarstrom.se> wrote:

> Den 2021-07-04 kl. 21:04 skrev Achim Gratz:
> > I tend to think that it might be possible to introduce "decorators" or
> > "traits" so that instead of the idiom above you'd be able to say
> > something like
> >
> > sort {:num:} @stuff;
> >
> > maybe even with stacking different decorations as in
> >
> > sort {:num:pre-sorted:stable:in-place:} @stuff;
>
> I would just do this:
>
> sub num { $a <=> $b }
> sort {num} @stuff;
>
> Put num and some similar functions in a module, call it List::Sort.
>
> Is there anything I'm missing?
>
>
sort { $a <=> $b } is currently optimized by the parser into a numeric sort
(we just don't have syntax for it). This results in significantly more
efficient operation than individually running an arbitrary code block on
every comparison. This could not easily be done with a custom sort
function, even from a core module.

-Dan