Sorry, I sent my answer just for you. So, the problem is you didn't call the same var you had declared.
my $foo = * **2; Then you call foo(2).say Missing the $ Try: $foo(2).say or say $foo(2) About the my @a = * **2; Your suggestion works @a[0](2) or @a[0].(2) But I would appreciate an explanation about why `$a[0](0)` didn't work. On Tue, Feb 11, 2020 at 9:45 PM Andy Bach <andy_b...@wiwb.uscourts.gov> wrote: > > I think it should be like this: > > > my $foo = * **2; > { ... } > > say $foo(4) > 16 > > That's what the doc says, but that's not what my install version says. I > do get > > my $foo = * **2; > { ... } > > but say foo get the "unknown sub" error > > > But I have another point:: > > > my @a = * **2; > > @a(2) > Invocant of method 'CALL-ME' must be a type object of type 'List', not an > object instance of type 'Array'. Did you forget a 'multi'? > in block <unit> at <unknown file> line 1 > Yeah, I'd be surprised if that worked > > > $a[0](2) > ===SORRY!=== Error while compiling: > Variable '$a' is not declared. Did you mean '@a'? > ------> <BOL>⏏$a[0](2) > > raku doesn't swap sigils anymore, so it should be > @a[0](2) > > maybe, pass the param, to the first bucket in @a which is holding a sub, > so run it - works here > > my @a = * **2; > [{ ... }] > > say @a[0](4); > 16 > > as does ".()" > > say @a[0].(5); > 25 > ------------------------------ > *From:* Aureliano Guedes <guedes.aureli...@gmail.com> > *Sent:* Tuesday, February 11, 2020 6:36 PM > *To:* Andy Bach <andy_b...@wiwb.uscourts.gov> > *Subject:* Re: variable as subroutine? > > I think it should be like this: > > > my $foo = * **2; > { ... } > > say $foo(4) > 16 > > But I have another point:: > > > my @a = * **2; > [{ ... }] > > @a(2) > Invocant of method 'CALL-ME' must be a type object of type 'List', not an > object instance of type 'Array'. Did you forget a 'multi'? > in block <unit> at <unknown file> line 1 > > > $a[0](2) > ===SORRY!=== Error while compiling: > Variable '$a' is not declared. Did you mean '@a'? > ------> <BOL>⏏$a[0](2) > > > > On Tue, Feb 11, 2020 at 8:43 PM Andy Bach <andy_b...@wiwb.uscourts.gov> > wrote: > > >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 <simon.proc...@gmail.com> > *Sent:* Tuesday, February 11, 2020 9:27 AM > *To:* Andy Bach <andy_b...@wiwb.uscourts.gov> > *Cc:* perl6-users <perl6-us...@perl.org> > *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 <andy_b...@wiwb.uscourts.gov> > 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 <simon.proc...@gmail.com> > *Sent:* Tuesday, February 11, 2020 3:17 AM > *To:* ToddAndMargo <toddandma...@zoho.com> > *Cc:* perl6-users <perl6-us...@perl.org> > *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 < > perl6-us...@perl.org> 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/ > > > > -- > Aureliano Guedes > skype: aureliano.guedes > contato: (11) 94292-6110 > whatsapp +5511942926110 > -- Aureliano Guedes skype: aureliano.guedes contact: (11) 94292-6110 WhatsApp +5511942926110