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:42
Subject:
Re: Any other way to do this
Message ID:
F5FE6D1B-A6D3-4572-8361-6EF6212DDC69@acm.org
Bill,

You were correct that `!=== 0` is redundant in the original code, because a numeric will be checked for zero-versus-not-zero in Boolean context, and because `==` and `===` should mean the same thing when comparing a bare numeric value.

In your latest version, I want to point out that `.words` should be redundant, since the shell will break up the command-line arguments on whitespace, before ever handing them to Raku.
Your code gives the same answer without `.words`.

Raku supports the procedural and functional (and OO, too!) paradigms very well.
Here is a different version, for readers who have not yet embraced the map/grep mindset we have been showing off.

my @nums;
for @*ARGS -> $arg {
    push @nums, $arg if $arg.Rat;
}
say @nums.sum;


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


> On Aug 31, 2020, at 3:02 PM, William Michels <wjm1@caa.columbia.edu> wrote:
> 
> Very nice, Bruce and Daniel!
> 
> I continued to hack on Rahakrishnan's code, then realized I could try
> using Bruce's grep() call as a filter:
> 
> ~$ raku -e '@*ARGS.words.grep(*.Rat).sum.say;'  100 200 300 apples
> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> 1195.7876
> 
> HTH, Bill.
> 
> On Mon, Aug 31, 2020 at 12: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