doc icon indicating copy to clipboard operation
doc copied to clipboard

Clarify use of Raku's 'any' versus that of Perl

Open tbrowder opened this issue 3 years ago • 10 comments

Given a Perl statement:

my $b = any { $_ < 0 } @args;

how does that translate to Raku?

tbrowder avatar Mar 12 '21 21:03 tbrowder

[email protected](* < 0 ) perhaps

jonathanstowe avatar Mar 12 '21 22:03 jonathanstowe

Oops, it's a Perl module function from CPAN List::Util.

tbrowder avatar Mar 12 '21 22:03 tbrowder

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)

Util avatar Mar 12 '21 22:03 Util

Maybe we could put this in one of the Perl pages?

JJ avatar Mar 13 '21 07:03 JJ

but because .first returns Nil on failure, I would state it as

Which is why the ? to boolify the Nil :-D

jonathanstowe avatar Mar 13 '21 14:03 jonathanstowe

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"

jonathanstowe avatar Mar 13 '21 14:03 jonathanstowe

And perhaps also note there is a Raku port of List::Util

lizmat avatar Mar 13 '21 14:03 lizmat

Yes, and it works great, along with Time::gmtime, both Butterfly modules thanks to our famous and prolific Raku developer @lizmat!!

tbrowder avatar Mar 13 '21 16:03 tbrowder

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.

coke avatar Mar 13 '21 23:03 coke

I'd taken it that this was intended for the 5-to-6 guides.

jonathanstowe avatar Mar 14 '21 10:03 jonathanstowe