Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
.assuming is provided by Code not Callable
Originally was provided by Block, but moved to Code in:
Rakudo fix: rakudo/rakudo@6e8dc6f132
Prop-spec:  Raku/roast@5bfe416140
Closes rakudo/rakudo#1735 R#1735
  • Loading branch information
zoffixznet committed Apr 14, 2018
1 parent 6208354 commit e0e9b14
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
53 changes: 0 additions & 53 deletions doc/Type/Callable.pod6
Expand Up @@ -49,59 +49,6 @@ Defined as:
Throws C<X::Cannot::Capture>.
=head2 method assuming
method assuming(Callable:D $self: |primers)
Returns a C<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($n){ my $i = 0; $i++ while $i < $n; $i };
# takes only one parameter and as such wont forward $n
sub bench(&c){ c, now - ENTER now };
say &slow.assuming(10000000).&bench; # OUTPUT: «(10000000 7.5508834)␤»
For a sub with arity greater than one, you can use C<Whatever> C<*> for all of the positional parameters that are not "assumed".
sub first-and-last ( $first, $last ) {
say "Name is $first $last";
}
my &surname-smith = &first-and-last.assuming( *, 'Smith' );
&surname-smith.( 'Joe' ); # OUTPUT: «Name is Joe Smith␤»
You can handle any combination of assumed and not assumed positional parameters:
sub longer-names ( $first, $middle, $last, $suffix ) {
say "Name is $first $middle $last $suffix";
}
my &surname-public = &longer-names.assuming( *, *, 'Public', * );
&surname-public.( 'Joe', 'Q.', 'Jr.'); # OUTPUT: «Name is Joe Q. Public Jr.␤»
Named parameters can be assumed as well:
sub foo { say "$^a $^b $:foo $:bar" }
&foo.assuming(13, :42foo)(24, :72bar); # OUTPUT: «13 24 42 72␤»
And you can use C<.assuming> on all types of L<Callables|/type/Callable>,
including L<Methods|/type/Method> and L<Blocks|/type/Block>:
# We use a Whatever star for the invocant:
my &comber = Str.^lookup('comb').assuming: *, /P \w+/;
say comber 'Perl is awesome! Python is great! And PHP is OK too';
# OUTPUT: «(Perl Python PHP)␤»
my &learner = {
"It took me $:months months to learn $^lang"
}.assuming: 'Perl 6';
say learner :6months; # OUTPUT: «It took me 6 months to learn Perl 6␤»
=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
53 changes: 53 additions & 0 deletions doc/Type/Code.pod6
Expand Up @@ -40,6 +40,59 @@ code object's C<Signature> do not contribute, nor do named parameters.
say &args.arity; # OUTPUT: «1␤»
say &slurpy.arity; # OUTPUT: «2␤»
=head2 method assuming
method assuming(Callable:D $self: |primers)
Returns a C<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($n){ my $i = 0; $i++ while $i < $n; $i };
# takes only one parameter and as such wont forward $n
sub bench(&c){ c, now - ENTER now };
say &slow.assuming(10000000).&bench; # OUTPUT: «(10000000 7.5508834)␤»
For a sub with arity greater than one, you can use C<Whatever> C<*> for all of the positional parameters that are not "assumed".
sub first-and-last ( $first, $last ) {
say "Name is $first $last";
}
my &surname-smith = &first-and-last.assuming( *, 'Smith' );
&surname-smith.( 'Joe' ); # OUTPUT: «Name is Joe Smith␤»
You can handle any combination of assumed and not assumed positional parameters:
sub longer-names ( $first, $middle, $last, $suffix ) {
say "Name is $first $middle $last $suffix";
}
my &surname-public = &longer-names.assuming( *, *, 'Public', * );
&surname-public.( 'Joe', 'Q.', 'Jr.'); # OUTPUT: «Name is Joe Q. Public Jr.␤»
Named parameters can be assumed as well:
sub foo { say "$^a $^b $:foo $:bar" }
&foo.assuming(13, :42foo)(24, :72bar); # OUTPUT: «13 24 42 72␤»
And you can use C<.assuming> on all types of L<Callables|/type/Callable>,
including L<Methods|/type/Method> and L<Blocks|/type/Block>:
# We use a Whatever star for the invocant:
my &comber = Str.^lookup('comb').assuming: *, /P \w+/;
say comber 'Perl is awesome! Python is great! And PHP is OK too';
# OUTPUT: «(Perl Python PHP)␤»
my &learner = {
"It took me $:months months to learn $^lang"
}.assuming: 'Perl 6';
say learner :6months; # OUTPUT: «It took me 6 months to learn Perl 6␤»
=head2 method count
Defined as:
Expand Down

0 comments on commit e0e9b14

Please sign in to comment.