Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adds new (and correct) definitions to map
This closes  #1731 but as a matter of fact was meant to address issue #2675.
The thing is map is just defined in Any (or frequently used in
lists). It does not belong to either and might be a good candidate for
the issue on independent routines: #2578 or #2070
  • Loading branch information
JJ committed Mar 27, 2019
1 parent 70630a8 commit 99c20ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
16 changes: 13 additions & 3 deletions doc/Type/Any.pod6
Expand Up @@ -166,17 +166,27 @@ Examples:
Defined as:
multi method map(Hash:D \hash)
multi method map(Iterable:D \iterable)
multi method map(|c)
multi method map(\SELF: █; :$label, :$item)
multi sub map(&code, +values)
C<map> will iterate over the invocant and apply the number of positional
parameters of the code object from the invocant per call. The returned values
of the code object will become elements of the returned L<Seq|/type/Seq>.
The C<:$label> and C<:$item> are useful only internally, since C<for> loops
get converted to C<map>s. The C<:$label> takes an existing L<Label|/type/Label> to label
the C<.map>'s loop with and C<:$item> controls whether the iteration will
The C<:$label> and C<:$item> are useful only internally, since C<for> loops get
converted to C<map>s. The C<:$label> takes an existing L<Label|/type/Label> to
label the C<.map>'s loop with and C<:$item> controls whether the iteration will
occur over C<(SELF,)> (if C<:$item> is set) or C<SELF>.
In C<sub> form, will apply the C<code> block to the C<values>, which will be
used as invocant.
The form with C<\c>, C<Iterable:D \iterable> and C<Hash:D \hash> as signatures
will fail with C<X::Cannot::Map>, and are mainly meant to catch common traps.
=head2 method deepmap
Defined as:
Expand Down
26 changes: 14 additions & 12 deletions doc/Type/List.pod6
Expand Up @@ -17,7 +17,7 @@ Arrays to have every value of the list stored in a container.
C<List> implements C<Positional> and as such provides support for
L<subscripts|/language/subscripts>.
=head1 Items, Flattening and Sigils
=head1 Items, flattening and sigils
In Perl 6, assigning a C<List> to a scalar variable does not lose information.
The difference is that iteration generally treats a list (or any other list-like
Expand Down Expand Up @@ -64,7 +64,7 @@ operation such as C<append>:
my @d = <a b>;
@d.append: $a; # The array variable @d has 3 elements, because
# $a is in an item context and as far as append is
# $a is in and as far as append is
# concerned a single element
say @d.elems; # OUTPUT: «3␤»
Expand Down Expand Up @@ -109,11 +109,12 @@ Defined as:
multi method ACCEPTS(List:D: $topic)
If C<$topic> is an L<Iterable|/type/Iterable>, returns C<True> or C<False> based on
whether the contents of the two C<Iterables> match. A L<Whatever|/type/Whatever>
element in the invocant matches anything in the corresponding position
of the C<$topic> C<Iterable>. A L<HyperWhatever|/type/HyperWhatever> matches any number of
any elements, including no elements:
If C<$topic> is an L<Iterable|/type/Iterable>, returns C<True> or C<False> based
on whether the contents of the two C<Iterables> match. A
L<Whatever|/type/Whatever> element in the invocant matches anything in the
corresponding position of the C<$topic> C<Iterable>. A
L<HyperWhatever|/type/HyperWhatever> matches any number of any elements,
including no elements:
say (1, 2, 3) ~~ (1, *, 3); # OUTPUT: «True␤»
say (1, 2, 3) ~~ (9, *, 5); # OUTPUT: «False␤»
Expand All @@ -129,9 +130,9 @@ In addition, returns C<False> if either the invocant or C<$topic>
L<is a lazy|/routine/is-lazy> C<Iterable>, unless C<$topic> is the same object
as the invocant, in which case C<True> is returned.
If C<$topic> is I<not> an L<Iterable|/type/Iterable>, returns the invocant if the invocant
has no elements or its first element is a L<Match|/type/Match> object (this behavior
powers C<m:g//> smartmatch), or C<False> otherwise.
If C<$topic> is I<not> an L<Iterable|/type/Iterable>, returns the invocant if
the invocant has no elements or its first element is a L<Match|/type/Match>
object (this behavior powers C<m:g//> smartmatch), or C<False> otherwise.
=head2 routine elems
Expand Down Expand Up @@ -275,8 +276,9 @@ Invokes C<&code> for each element and gathers the return values in a
sequence and returns it. This happens lazily, i.e. C<&code> is only
invoked when the return values are accessed.Examples:
say ('hello', 1, 22/7, 42, 'world').map: { .^name } # OUTPUT: «(Str Int Rat Int Str)␤»
say map *.Str.chars, 'hello', 1, 22/7, 42, 'world'; # OUTPUT: «(5 1 8 2 5)␤»
=for code
say ('hello', 1, 22/7, 42, 'world').map: { .^name } # OUTPUT: «(Str Int Rat Int Str)␤»
say map *.Str.chars, 'hello', 1, 22/7, 42, 'world'; # OUTPUT: «(5 1 8 2 5)␤»
C<map> inspects the arity of the code object, and tries to pass as many
arguments to it as expected:
Expand Down

0 comments on commit 99c20ef

Please sign in to comment.