Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can I use the Whatever star?

Tags:

currying

raku

Following this post on perlgeek, it gives an example of currying:

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

Makes sense. But if I swap 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 trying to call + via the infix syntax fails:

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

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

[Edited with responses from newer rakudo; Version string for above: perl6 version 2014.08 built on MoarVM version 2014.08]

like image 761
Phil H Avatar asked Sep 04 '14 15:09

Phil H


Video Answer


1 Answers

Your Rakudo version is somewhat ancient. If you want to use a more recent cygwin version, you'll probably have to compile it yourself. If you're fine with the Windows version, you can get a binary from rakudo.org.

That said, the current version also does not transform * min 2 into a lambda, but from a cursory test, seems to treat the * like Inf. My Perl6-fu is too weak to know if this is per spec, or a bug.

As a workaround, use

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

Note that * only auto-curries (or rather 'auto-primes' in Perl6-speak - see S02) with operators, not function calls, ie your 3rd example should be written as

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

This is because the meaning of the Whatever-* depends on context: it is supposed to DWIM.

In case of function calls, it gets passed as an argument to let the callee decide what it wants to do with it. Even operators are free to handle Whatever explicitly (eg 1..*) - but if they don't, a Whatever operand transforms the operation into a 'primed' closure.

like image 124
Christoph Avatar answered Sep 24 '22 04:09

Christoph