doc
doc copied to clipboard
Clarify use of Raku's 'any' versus that of Perl
Given a Perl statement:
my $b = any { $_ < 0 } @args;
how does that translate to Raku?
[email protected](* < 0 )
perhaps
Oops, it's a Perl module function from CPAN List::Util.
On Mar 12, 2021, at 3:32 PM, Tom Browder @.***> wrote:
Given a Perl statement:
my $b = any { $_ < 0 } @args;
how does that translate to Raku?
my $b = so @a.any < 0;
raku -e 'my @a = 0, 2, 5; say so @a.any < 0;' False
raku -e 'my @a = 0, -2, 5; say so @a.any < 0;' True
https://docs.raku.org/routine/so https://docs.raku.org/routine/any https://docs.raku.org/type/Junction
The so
is needed to collapse the Junction to a simple Boolean (and probably allow short-circuiting):
raku -e 'my @a = 0, -2, 5; say @a.any < 0;'
any(False, True, False)
A question-mark boolean coercer would be too high-precedence to work there without parens.
Jonathan Stowe’s suggestion of: @.*( < 0) also looks good to me, but because .first returns Nil on failure, I would state it as: @args.first( < 0).defined
-- Hope this helps, Bruce Gray (Util of PerlMonks)
Maybe we could put this in one of the Perl pages?
but because .first returns Nil on failure, I would state it as
Which is why the ?
to boolify the Nil
:-D
Maybe we could put this in one of the Perl pages?
Yeah, List::Util
is virtually ubiquitous. It's almost like there should be a page with "things that are builtin in raku that required modules in Perl"
And perhaps also note there is a Raku port of List::Util
Yes, and it works great, along with Time::gmtime, both Butterfly modules thanks to our famous and prolific Raku developer @lizmat!!
From the style guide:
### Don't reference Perl unless in a 5-to-6 document or related document
We are not expecting our users to have to know Perl to learn Raku,
so this should not be part of the bulk of the documentation.
I'd taken it that this was intended for the 5-to-6 guides.