Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: A proper name for is_sorted function that can check more than just sorting order?

by Anonymous Monk
on Dec 26, 2017 at 13:45 UTC ( [id://1206203]=note: print w/replies, xml ) Need Help??


in reply to A proper name for is_sorted function that can check more than just sorting order?

There is a "stupid-simple" way to write this as a sub which returns True or False. Something like:
$a = <file>; while ($b = <file>) { if (($a cmp $b) > 0) return false; $a = $b; } return true;
There. Anyone can understand that in about ten seconds, and debug it and patch it forever. Go ahead and write as many "redundant" functions like this as you care to, because the day might will come when one of them has to be just a little different ... and you prepared for that, so what could be a big change in "clever" code is a small one instead, just as it should be. Your successors will thank you.
  • Comment on Re: A proper name for is_sorted function that can check more than just sorting order?
  • Download Code

Replies are listed 'Best First'.
Re^2: A proper name for is_sorted function that can check more than just sorting order?
by LanX (Saint) on Dec 28, 2017 at 08:09 UTC
Re^2: A proper name for is_sorted function that can check more than just sorting order?
by Dallaylaen (Chaplain) on Dec 28, 2017 at 09:18 UTC

    Generally I prefer to use simple constructs, especially in tests. However, sometimes a well-defined wrapper is better. Something like Test::Warn:

    warnings_like { my_fucntion() } [ {carped => qr/deprecated.*our_function/} ], "Warning issued, alter +native suggested";

    This can be written with $SIG{__WARN__} and like, but why bother? The intention is clear from this code.

    My proposed "test that condition holds for all adjacent values in an array" looks pretty well-defined to me. And if it's insufficient, there's always subtest and a for loop...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-26 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found