develooper Front page | perl.perl5.porters | Postings from July 2021

Feature request / RFC: builtin for numeric sort

Thread Previous | Thread Next
From:
Ed Avis
Date:
July 1, 2021 07:04
Subject:
Feature request / RFC: builtin for numeric sort
Message ID:
DM4PR11MB5247E256E03F87049C95D75F9D009@DM4PR11MB5247.namprd11.prod.outlook.com
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.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About