Skip to content

Instantly share code, notes, and snippets.

@dogbert17
Last active June 17, 2017 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dogbert17/bdc044f30b5cf3e387e07cdd84ab2694 to your computer and use it in GitHub Desktop.
Save dogbert17/bdc044f30b5cf3e387e07cdd84ab2694 to your computer and use it in GitHub Desktop.
Attempt to document nodemap
=head2 method nodemap
Defined as:
method nodemap(&block --> List) is nodal
C<nodemap> will apply C<&block> to each element and return a new C<List> with
the return values of C<&block>. In contrast to C<deepmap> it will B<not> descend
recursively into sublists if it finds elements which does the C<Iterable> role.
say [[1,2,3], [[4,5],6,7], 7].nodemap(*+1);
# OUTPUT: «(4, 4, 8)␤»
say [[2, 3], [4, [5, 6]]]».nodemap(*+1)
# OUTPUT: «((3 4) (5 3))␤»
The examples above would have produced the exact same results if we had used
C<map> instead of C<nodemap>. The difference between the two lies in the
fact that C<map> can handle L<slips|/type/Slip> something which C<nodemap>
can't.
say [[2,3], [[4,5],6,7], 7].nodemap({.elems == 1 ?? $_ !! slip});
# OUTPUT: «(() () 7)␤»
say [[2,3], [[4,5],6,7], 7].map({.elems == 1 ?? $_ !! slip});
# OUTPUT: «(7)␤»
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment