Invocable code object
Callables can be stored in
&-sigiled containers, the default type constraint of such a container is
Callable. A signature object can be used to force a check against the signature of the Callable to be stored into the container.
my = ; # Empty block needs a semicolonmy = -> ;my = sub () ;sub foo() ;my = ;
my :(Int) = sub bar(Int) ; # Not yet implementedmy :(Str) = -> Str ; # Not yet implemented
method CALL-ME(Callable : |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.
does Callablemy = A;say a(); # OUTPUT: «called␤»
method assuming(Callable : |primers)
Callable that implements the same behaviour as the original, but has the values passed to .assuming already bound to the corresponding parameters.
my sub slow();# takes only one parameter and as such wont forward $nsub bench();say .assuming(10000000).; # OUTPUT: «(10000000 7.5508834)␤»
For a sub with arity greater than one, you can use
* for all of the positional parameters that are not "assumed".
sub first-and-last ( , )my = .assuming( *, 'Smith' );.( 'Joe' ); # OUTPUT: «Name is Joe Smith␤»
You can handle any combination of assumed and not assumed positional parameters:
sub longer-names ( , , , )my = .assuming( *, *, 'Public', * );.( 'Joe', 'Q.', 'Jr.'); # OUTPUT: «Name is Joe Q. Public Jr.␤»
Named parameters can be assumed as well:
sub foo.assuming(13, :42foo)(24, :72bar); # OUTPUT: «13 24 42 72␤»
# We use a Whatever star for the invocant:my = Str.^lookup('comb').assuming: *, /P \w+/;say comber 'Perl is awesome! Python is great! And PHP is OK too';# OUTPUT: «(Perl Python PHP)␤»my =.assuming: 'Perl 6';say learner :6months; # OUTPUT: «It took me 6 months to learn Perl 6␤»
The function composition operator
infix:<o> combines two functions, so that the left function is called with the return value of the right function. If the
.count of the left function is greater than 1, the return value of the right function will be slipped into the left function.
.arity of the RHS will be maintained.
sub f()sub g()my = ∘ ;say composed 2; # OUTPUT: «g␤f␤2␤»# equivalent to:say 2..;# or to:say f g 2;
sub f(, , )sub g()my = ∘ ;say composed 'abc'; # OUTPUT: «cba␤»# equivalent to:say f |g 'abc';