Skip to content

Commit cde810e

Browse files
authored
deepmap/duckmap/nodemap, issues #4560 & #4711 (#4718)
* addressed issue #4711 with minimal changes Added a mention of the third example to smooth the abrupt transition, and selected only one example of either method or sub call as most appropriate for each. * sub forms of deepmap and duckmap are not multi * issue #4560 for routine nodemap * leap seconds (issue #3881)
1 parent c747ce5 commit cde810e

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

doc/Type/Any.rakudoc

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ the L<C<Seq>|/type/Seq> will be iterating over its elements, calling C<.take> on
189189
=head2 routine deepmap
190190

191191
method deepmap(&block --> Iterable) is nodal
192-
multi deepmap(&op, \obj --> Iterable)
192+
sub deepmap(&op, \obj --> Iterable)
193193

194194
C<deepmap> will apply its argument (or first argument for the sub form) to
195195
each element and return a new L<C<Iterable>|/type/Iterable> with the return
@@ -213,7 +213,7 @@ say deepmap *.flip, {what=>'is', this=>'thing', a=><real list>};
213213
=head2 routine duckmap
214214

215215
method duckmap(&block --> Iterable) is rw is nodal
216-
multi duckmap(&op, \obj --> Iterable)
216+
sub duckmap(&op, \obj --> Iterable)
217217

218218
C<duckmap> will apply its argument (or first argument for the sub form) to each
219219
element that behaves in such a way that the argument can be applied. If it
@@ -224,57 +224,49 @@ L<C<Associative>|/type/Associative>.
224224
=for code
225225
<a b c d e f g>.duckmap(-> $_ where <c d e>.any { .uc }).say;
226226
# OUTPUT: «(a b C D E f g)␤»
227-
say duckmap -> $_ where <c d e>.any { .uc }, <a b c d e f g>;
228-
# OUTPUT: «(a b C D E f g)␤»
229227
(('d', 'e'), 'f').duckmap(-> $_ where <e f>.any { .uc }).say;
230228
# OUTPUT: «((d E) F)␤»
231-
say duckmap -> $_ where <e f>.any { .uc }, (('d', 'e'), 'f');
232-
# OUTPUT: «((d E) F)␤»
233229
{ first => ('d', 'e'), second => 'f'}.duckmap(-> $_ where <e f>.any { .uc }).say;
234230
# OUTPUT: «{first => (d E), second => F}␤»
235-
say duckmap -> $_ where <e f>.any { .uc }, { first => ('d', 'e'), second => 'f'};
236-
# OUTPUT: «{first => (d E), second => F}␤»
237231

238232
In the first case, it is applied to C<c>, C<d> and C<e> which are the ones that
239233
meet the conditions for the block (C<{ .uc }>) to be applied; the rest are
240234
returned as is.
241235

242236
In the second case, the first item is a list that does not meet the condition,
243-
so it's visited; that flat list will behave in the same way as the first one. In
244-
this case:
237+
so it's visited; that flat list will behave in the same way as the first one.
238+
Similarly in the third case; here it's only the values in each pair that are
239+
visited.
245240

246-
say [[1,2,3],[[4,5],6,7]].duckmap( *² ); # OUTPUT: «[9 9]␤»
247241
say duckmap *², [[1,2,3],[[4,5],6,7]]; # OUTPUT: «[9 9]␤»
248242

249-
250243
You can square anything as long as it behaves like a number. In this case, there
251244
are two arrays with 3 elements each; these arrays will be converted into the
252245
number 3 and squared. In the next case, however
253246

254-
say [[1,2,3],[[4,5],6.1,7.2]].duckmap( -> Rat $_ { $_²} );
255-
# OUTPUT: «[[1 2 3] [[4 5] 37.21 51.84]]␤»
256247
say duckmap -> Rat $_ { $_²}, [[1,2,3],[[4,5],6.1,7.2]];
257248
# OUTPUT: «[[1 2 3] [[4 5] 37.21 51.84]]␤»
258249

259250
3-item lists are not L<C<Rat>|/type/Rat>, so it descends recursively, but eventually only
260-
applies the operation to those that walk (or slither, as the case may be) like a
261-
L<C<Rat>|/type/Rat>.
251+
applies the operation to those that match L<C<Rat>|/type/Rat>.
262252

263253
Although on the surface (and name), C<duckmap> might look similar to
264254
L<C<deepmap>|/routine/deepmap>, the latter is applied recursively regardless of
265255
the type of the item.
266256

267-
=head2 method nodemap
257+
=head2 routine nodemap
268258

269-
method nodemap(&block --> List) is nodal
259+
method nodemap(&block --> Iterable) is nodal
260+
sub nodemap(&op, \obj --> Iterable)
270261

271-
C<nodemap> will apply C<&block> to each element and return a new
272-
L<C<List>|/type/List> with the return values of C<&block>. In contrast to
262+
C<nodemap> will apply its argument (or first argument for the sub form) to each
263+
element and return a new L<C<Iterable>|/type/Iterable> with the return values
264+
of its L<C<Callable>|/type/Callable> argument. In contrast to
273265
L<deepmap|/routine/deepmap> it will B<not> descend recursively into sublists if
274266
it finds elements which L<do|/routine/does> the L<C<Iterable>|/type/Iterable>
275267
role.
276268

277-
say [[1,2,3], [[4,5],6,7], 7].nodemap(*+1);
269+
say nodemap *+1, [[1,2,3], [[4,5],6,7], 7];
278270
# OUTPUT: «(4, 4, 8)␤»
279271

280272
say [[2, 3], [4, [5, 6]]]».nodemap(*+1)
@@ -292,7 +284,7 @@ C<nodemap> doesn't.
292284

293285
When applied to L<C<Associative>|/type/Associative>s, it will act on the values:
294286

295-
{ what => "is", this => "thing" }.nodemap( *.flip ).say;
287+
say nodemap *.flip, { what => "is", this => "thing" };
296288
# OUTPUT: «{this => gniht, what => si}␤»
297289

298290
=head2 method flat

doc/Type/Mu.rakudoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ L<react|/language/concurrency#react> block.
497497

498498
=head2 routine take
499499

500-
sub take(\item)
501500
method take()
501+
sub take(\item)
502502

503503
The sub takes the given item and passes it to the enclosing C<gather>
504504
block.

0 commit comments

Comments
 (0)