develooper Front page | perl.perl6.users | Postings from August 2020

Re: Any other way to do this

Thread Previous | Thread Next
From:
Bruce Gray
Date:
August 31, 2020 20:25
Subject:
Re: Any other way to do this
Message ID:
670F2970-C5FC-4E42-A6F8-F25A49D54B3B@acm.org
Daniel’s solution taught me to not ignore the simple numeric coercion operator: `+`.
I was trying to improve version just now, using `0 + $_` in a grep, and got hung up on the error thrown on failed numeric conversion.
I ended up with:
	.grep: { try 0 + $_ };
Which does work, and might even be better than .grep(+*) , but I totally missed the coercion as a shortcut.

A few other thoughts:
	* The original code fails to handle `0.01`, because (0.01).Int is exactly Zero, which fails the `!=== 0` before .Rat is ever called.
	* My Regex fails to handle negative numbers, just due to my oversight. It also does not handle 0.3 expressed without the leading zero: `.3`
	* All the non-regex solutions (including the original) have unexpected effects due to the use of Raku’s numeric conversion; none of these are rejected:
		0xDEADBEEF
		1e15
		0b11
		-0o77
		15/60
	* If the job of the code is “# sums the numbers…”, then we might need a clearer definition of Number.
		Is it OK to accept whatever Raku can understand as a number?
		If not, then a stricter filter must be implemented.

I finally settled on using `try` instead of numeric coercion, because if I am not golfing, I think `try` makes the grep look more like “filter out the non-numbers” instead of “get rid of the zero values”.
Also, I am using a `sub MAIN` with a comment that binds to `WHY` to give a better command-line usage message, and using named intermediate variables to clarify what we are trying to achieve.


#|(Print the sum of the command-line arguments, but only those arguments that are valid numbers)
sub MAIN ( *@possible_numbers ) {
    die $*USAGE if not @possible_numbers;

    my @actual_numbers = @possible_numbers.grep: { try 0 + $_ };
    
    say @actual_numbers.sum;
}


Calling with a `—help` flag, or without arguments, now gives:
$ raku ~/T/d20200831/ee25_e.raku --help
Usage:
  /Users/bruce_pro/T/d20200831/ee25_e.raku [<possible_numbers> ...] -- Print the sum of the command-line arguments, but only those arguments that are valid numbers

— 
Hope this helps,
Bruce Gray (Util of PerlMonks)

> On Aug 31, 2020, at 2:23 PM, daniel@codesections.com wrote:
> 
> I like Bruce's Regex-based approach.
> 
> Here's how I'd probably approach the problem:
> 
> raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kilos
> 18.7876 500 grams14 10stars10 sun100moon 77
> 
> August 31, 2020 2:28 PM, "Bruce Gray" <robertbrucegray3@gmail.com> wrote:
> 
>> my $is_a_number = / ^ \d+ [ '.' \d* ]? $ /;
>> 
>> my $sum = @*ARGS.grep($is_a_number).sum;
>> 
>> say $sum;
>> 
>> — 
>> Hope this helps,
>> Bruce Gray (Util of PerlMonks)
>> 
>>> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users <perl6-users@perl.org> wrote:
>>> 
>>> I think it looks very good, Radhakrishnan! Presumably you are happy
>>> with the sum 1195.7876?
>>> 
>>> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
>>> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
>>> 100
>>> 200
>>> 300
>>> 18.7876
>>> 500
>>> 77
>>> 
>>> I'm still mulling over whether or not the "!=== 0" is essential. I
>>> have yet to mess-up the command line arguments sufficiently to require
>>> it, and throwing a zero onto the command line seems to be handled
>>> gracefully.
>>> 
>>> Anyone else want to chime in?
>>> 
>>> Best, Bill.
>>> 
>>> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
>>> <weorkay@gmail.com> wrote:
>>>> Please see the following script that checks for type and sums up only the numbers passed as
>>>> arguments to the script in the command line. I would be grateful if any improvement or furtherance
>>>> to this script is offered. Thank you.
>>>> 
>>>> #
>>>> # sums the numbers given in command line arguments and prints
>>>> #
>>>> my $sum = 0;
>>>> for @*ARGS
>>>> {
>>>> $sum += $_.Rat if $_.Int // 0 !=== 0;
>>>> }
>>>> say $sum;
>>>> 
>>>> #
>>>> # command line execution
>>>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
>>>> #

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