Skip to content

Commit 99c20ef

Browse files
committed
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
1 parent 70630a8 commit 99c20ef

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

doc/Type/Any.pod6

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,27 @@ Examples:
166166
167167
Defined as:
168168
169+
multi method map(Hash:D \hash)
170+
multi method map(Iterable:D \iterable)
171+
multi method map(|c)
169172
multi method map(\SELF: █; :$label, :$item)
173+
multi sub map(&code, +values)
170174
171175
C<map> will iterate over the invocant and apply the number of positional
172176
parameters of the code object from the invocant per call. The returned values
173177
of the code object will become elements of the returned L<Seq|/type/Seq>.
174178
175-
The C<:$label> and C<:$item> are useful only internally, since C<for> loops
176-
get converted to C<map>s. The C<:$label> takes an existing L<Label|/type/Label> to label
177-
the C<.map>'s loop with and C<:$item> controls whether the iteration will
179+
The C<:$label> and C<:$item> are useful only internally, since C<for> loops get
180+
converted to C<map>s. The C<:$label> takes an existing L<Label|/type/Label> to
181+
label the C<.map>'s loop with and C<:$item> controls whether the iteration will
178182
occur over C<(SELF,)> (if C<:$item> is set) or C<SELF>.
179183
184+
In C<sub> form, will apply the C<code> block to the C<values>, which will be
185+
used as invocant.
186+
187+
The form with C<\c>, C<Iterable:D \iterable> and C<Hash:D \hash> as signatures
188+
will fail with C<X::Cannot::Map>, and are mainly meant to catch common traps.
189+
180190
=head2 method deepmap
181191
182192
Defined as:

doc/Type/List.pod6

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Arrays to have every value of the list stored in a container.
1717
C<List> implements C<Positional> and as such provides support for
1818
L<subscripts|/language/subscripts>.
1919
20-
=head1 Items, Flattening and Sigils
20+
=head1 Items, flattening and sigils
2121
2222
In Perl 6, assigning a C<List> to a scalar variable does not lose information.
2323
The difference is that iteration generally treats a list (or any other list-like
@@ -64,7 +64,7 @@ operation such as C<append>:
6464
6565
my @d = <a b>;
6666
@d.append: $a; # The array variable @d has 3 elements, because
67-
# $a is in an item context and as far as append is
67+
# $a is in and as far as append is
6868
# concerned a single element
6969
7070
say @d.elems; # OUTPUT: «3␤»
@@ -109,11 +109,12 @@ Defined as:
109109
110110
multi method ACCEPTS(List:D: $topic)
111111
112-
If C<$topic> is an L<Iterable|/type/Iterable>, returns C<True> or C<False> based on
113-
whether the contents of the two C<Iterables> match. A L<Whatever|/type/Whatever>
114-
element in the invocant matches anything in the corresponding position
115-
of the C<$topic> C<Iterable>. A L<HyperWhatever|/type/HyperWhatever> matches any number of
116-
any elements, including no elements:
112+
If C<$topic> is an L<Iterable|/type/Iterable>, returns C<True> or C<False> based
113+
on whether the contents of the two C<Iterables> match. A
114+
L<Whatever|/type/Whatever> element in the invocant matches anything in the
115+
corresponding position of the C<$topic> C<Iterable>. A
116+
L<HyperWhatever|/type/HyperWhatever> matches any number of any elements,
117+
including no elements:
117118
118119
say (1, 2, 3) ~~ (1, *, 3); # OUTPUT: «True␤»
119120
say (1, 2, 3) ~~ (9, *, 5); # OUTPUT: «False␤»
@@ -129,9 +130,9 @@ In addition, returns C<False> if either the invocant or C<$topic>
129130
L<is a lazy|/routine/is-lazy> C<Iterable>, unless C<$topic> is the same object
130131
as the invocant, in which case C<True> is returned.
131132
132-
If C<$topic> is I<not> an L<Iterable|/type/Iterable>, returns the invocant if the invocant
133-
has no elements or its first element is a L<Match|/type/Match> object (this behavior
134-
powers C<m:g//> smartmatch), or C<False> otherwise.
133+
If C<$topic> is I<not> an L<Iterable|/type/Iterable>, returns the invocant if
134+
the invocant has no elements or its first element is a L<Match|/type/Match>
135+
object (this behavior powers C<m:g//> smartmatch), or C<False> otherwise.
135136
136137
=head2 routine elems
137138
@@ -275,8 +276,9 @@ Invokes C<&code> for each element and gathers the return values in a
275276
sequence and returns it. This happens lazily, i.e. C<&code> is only
276277
invoked when the return values are accessed.Examples:
277278
278-
say ('hello', 1, 22/7, 42, 'world').map: { .^name } # OUTPUT: «(Str Int Rat Int Str)␤»
279-
say map *.Str.chars, 'hello', 1, 22/7, 42, 'world'; # OUTPUT: «(5 1 8 2 5)␤»
279+
=for code
280+
say ('hello', 1, 22/7, 42, 'world').map: { .^name } # OUTPUT: «(Str Int Rat Int Str)␤»
281+
say map *.Str.chars, 'hello', 1, 22/7, 42, 'world'; # OUTPUT: «(5 1 8 2 5)␤»
280282
281283
C<map> inspects the arity of the code object, and tries to pass as many
282284
arguments to it as expected:

0 commit comments

Comments
 (0)