>The * * * call generates a WhateverCode block. This is expecting 2 arguments.
-> $x { $x * $x } is taking one argument.
> The best documentation would probably be : https://docs.raku.org/type/Whatever
so, from:
Multiple * in one expression generate closures with as many arguments:
my $c = * + *; # same as -> $x, $y { $x + $y }
Using * in complex expressions will also generate closures:
my $c = 4 * * + 5; # same as -> $x { 4 * $x + 5 }
The * * * the parser says "one whatever, one math op (*) and one more whatever"
my $foo = $x, $y { $x + $y };
so,
my $foo = * **2;
should do $x * $x? Though I see
> my $foo = * **2;
{ ... }
say foo(4);
===SORRY!=== Error while compiling:
Undeclared routine:
foo used at line 1
but '&' works
> my &foo = * **2;
{ ... }
> foo(4);
16
> my &c = * **2;
{ ... }
> say c(4);
16
________________________________
From: Simon Proctor <[email protected]>
Sent: Tuesday, February 11, 2020 9:27 AM
To: Andy Bach <[email protected]>
Cc: perl6-users <[email protected]>
Subject: Re: variable as subroutine?
The * * * call generates a WhateverCode block. This is expecting 2 arguments.
-> $x { $x * $x } is taking one argument.
The best documentation would probably be : https://docs.raku.org/type/Whatever
Hope that helps.
(For giggles earlier I made this dumb example of functional programming)
my &ident = {$_};
my &sq = {$_ * $_};
sub trinar( &test, &true, &false, *@values ) { @values.map( -> $v { &test($v)
?? &true($v) !! &false($v) } ) };
trinar( *.is-prime, &sq,&ident, ^30 ).say
Enjoy. ;)
On Tue, 11 Feb 2020 at 15:22, Andy Bach
<[email protected]<mailto:[email protected]>> wrote:
I have a few less related questions
>> those are 3 ways to write the same sub:
sub foo ($x) { $x * $x }
my &foo = -> $x { $x * $x }
my &foo = * * *;
> A Note on Marc's comment:
my &foo = * * *
is not the same as:
my &foo = -> $x { $x * $x }
it is the same as:
my &foo = -> $x, $y { $x * $y }
Okay, "* * *" - how does that work? How is it different than
-> $x { $x * $x }
? It needs two params?
I followed the callable link but that left me with more questions:
method CALL-ME
method CALL-ME(Callable:D $self: |arguments)
This method is required for postfix:«( )» and postfix:«.( )». It's what makes
an object actually call-able and needs to be overloaded to let a given object
act like a routine. If the object needs to be stored in a &-sigiled container,
is has to implement Callable.
class A does Callable {
submethod CALL-ME(|c){ 'called' }
}
my &a = A;
say a(); # OUTPUT: «called»
That second "postfix" operator, means
say a.(); # also outputs "called"
but what is the "pipe c" signature doing for the submethod?
________________________________
From: Simon Proctor <[email protected]<mailto:[email protected]>>
Sent: Tuesday, February 11, 2020 3:17 AM
To: ToddAndMargo <[email protected]<mailto:[email protected]>>
Cc: perl6-users <[email protected]<mailto:[email protected]>>
Subject: Re: variable as subroutine?
If you can store a subroutine in a variable then you can pass said subroutine
to another one as an argument.
This leads us into the joys of functional programming.
And you may have used it already and not even realised.
The .map and .grep methods (and .reduce and bunch of others) all expect a
callable code block (that might be a subroutine) as a function.
This :
my @a = (1..10).map( * ** 2 )
and this :
my &sq = sub ($v) { $v ** 2 };
my @a = (1..10).map( &sq );
are doing the same thing. Except the second one has the &sq function available
for other things.
(A Note on Marc's comment * * * is not the same as -> $x { $x * $x } it is the
same as -> $x, $y { $x * $y } )
You can then start doing things like storing functions as values in hashes and
doing all *kinds* of fun stuff.
Welcome to the tip of the iceberg.
Simon
On Tue, 11 Feb 2020 at 03:21, ToddAndMargo via perl6-users
<[email protected]<mailto:[email protected]>> wrote:
Hi All,
Is Larry using his magic powder again?
Can I declare a subroutine as a variable?
my $abc = my sub (UInt $u, Str $s, Int $I) {
How would I use it?
And why would do such a thing?
-T
--
Simon Proctor
Cognoscite aliquid novum cotidie
http://www.khanate.co.uk/
--
Simon Proctor
Cognoscite aliquid novum cotidie
http://www.khanate.co.uk/