When can I use any star?

Following this entry in perlgeek , he gives an example currying:

my &add_two := * + 2; say add_two(5); # 7 

Has the meaning. But if I change the infix operator + for the min infix operator:

 my &min_two := * min 2; say min_two(5); # Type check failed in binding; expected 'Callable' but got 'Int' 

Even an attempt to invoke + using the infix syntax fails:

 >> my &curry := &infix:<+>(2, *); Method 'Int' not found for invocant of class 'Whatever' 

Do I need to qualify any value as a numeric value, and if so, how? Or am I completely missing the point?

[Edited with answers from the new rakudo; Version string above: perl6 version 2014.08 built on MoarVM version 2014.08 ]

+6
source share
1 answer

Your version of Rakudo is somewhat ancient. If you want to use the newer version of cygwin, you probably have to compile it yourself. If you're ok with the Windows version, you can get the binary from rakudo.org .

However, the current version also does not convert * min 2 to lambda, but from a quick test it seems to refer to * as Inf . My Perl6-fu is too weak to find out if it matches the specification or the error.

As a workaround, use

 my &min_two := { $_ min 2 }; 

Please note that * only autocarnes (or, rather, "autotests" in Perl6-talk - see S02 ) with operators, not function calls, i.e. your third example should be written as

 my &curry := &infix:<+>.assuming(2); 

This is because the Whatever- * value depends on the context: DWIM is assumed.

In the case of function calls, it is passed as an argument, allowing the callee to decide what he wants to do with it. Even operators can freely access regardless of what is explicit (for example, 1..* ), but if they do not, any operand turns the operation into a β€œgrounded” closure.

+3
source

Source: https://habr.com/ru/post/974838/


All Articles