Documentation for routine
map assembled from the following types:
multi method map(\SELF: ;; :, :)multi sub map(, +values)multi method map(Hash \hash)multi method map(Iterable \iterable)multi method map(|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 Seq.
:$item are useful only internally, since
for loops get converted to
:$label takes an existing Label to label the
.map's loop with and
:$item controls whether the iteration will occur over
:$item is set) or
sub form, it will apply the
code block to the
values, which will be used as invocant.
The forms with
Iterable:D \iterable and
Hash:D \hash as signatures will fail with
X::Cannot::Map, and are mainly meant to catch common traps.
for statement that has been sunk, a
Seq created by a map will also sink:
say gather for 1# OUTPUT: «(0 1 2)␤»
In this case,
gather sinks the
for statement, and the result of sinking the
Seq will be iterating over its elements, calling
.take on them.
method map(Supply: --> Supply)
Returns a new supply that maps each value of the given supply through
&mapper and emits it to the new supply.
my = Supplier.new;my = .Supply;my = .map(-> );.tap();.emit(4); # OUTPUT: «8»
method map(RaceSeq: , *)
Uses maps on the
RaceSeq, generally created by application of
.race to a preexisting
multi method map(Hash \hash)multi method map(Iterable \iterable)multi method map(|c)multi method map(\SELF: ;; :, :)multi sub map(, +values)
Examples applied to lists are included here for the purpose of illustration.
For a list, it invokes
&code for each element and gathers the return values in a sequence and returns it. This happens lazily, i.e.
&code is only invoked when the return values are accessed.Examples:
say ('hello', 1, 22/7, 42, 'world').map: # OUTPUT: «(Str Int Rat Int Str)␤»say map *.Str.chars, 'hello', 1, 22/7, 42, 'world'; # OUTPUT: «(5 1 8 2 5)␤»
map inspects the arity of the code object, and tries to pass as many arguments to it as expected:
sub b(, ) ;say <a b x y>.map().join(', '); # OUTPUT: «a before b, x before y␤»
iterates the list two items at a time.
map does not flatten embedded lists and arrays, so
((1, 2), <a b>).map()
(1, 2) and
<a b> in turn to the block, leading to a total of two iterations and the result sequence
"1,2", "a,b". See method flatmap for an alternative that flattens.
&code is a Block loop phasers will be executed and loop control statements will be treated as in loop control flow. Please note that
return is executed in the context of its definition. It is not the return statement of the block but the surrounding Routine. Using a Routine will also handle loop control statements and loop phasers. Any
Routine specific control statement or phaser will be handled in the context of that
sub s;s# OUTPUT: «hi␤»
method map(HyperSeq: , *)
Uses maps on the
HyperSeq, generally created by application of
hyper to a preexisting
multi method map(Backtrace: --> Seq)
&block for each element and gathers the return values in a sequence and returns it.
sub innersub outerouter;
SETTING::src/core.c/Backtrace.pm6: 85SETTING::src/core.c/Backtrace.pm6: 85test.p6: 1test.p6: 2test.p6: 3test.p6: 1