method flatmap

Documentation for method flatmap assembled from the following types:

class Any

From Any

(Any) method flatmap

Defined as:

method flatmap(&block:$label)

DEPRECATION NOTICE: This method is deprecated in 6.d and will be removed in 6.e. Use .map followed by .flat instead.

Applies map to every element with the block and Label used as an argument and flattens out the result using .flat.

say "aabbccc".comb.Mix.flatmap: "→ " ~ *# OUTPUT: «(→ b␉2 → c␉3 → a␉2)␤»

In this case, the elements of the Mix are itemized to key␉value, and then mapped and flattened. Same result as

say "aabbccc""→ " ~ * ).flat

Which is why it is deprecated in 6.d and will be eventually eliminated in 6.e.

class List

From List

(List) method flatmap

Defined as:

method flatmap(List:D: &code --> Seq:D)

Like map iterates over the elements of the invocant list, feeding each element in turn to the code reference, and assembling the return values from these invocations in a result list.

The use of flatmap is strongly discouraged. Instead of .flatmap( ), please use .map( ).flat as it is clear when the .flat is called and is not confusing like .flatmap.

Unlike map it flattens non-itemized lists and arrays, so

## flatmap 
my @list = ('first1', ('second2', ('third3''third4'), 'second5'), 'first6');
say @list.flatmap({.reverse}).perl;
# OUTPUT «("first1", "second5", "third3", "third4", "second2", "first6").Seq␤» 
## map 
say{"$_ was a {.^name}"}).perl;
# OUTPUT «("first1 was a Str", "second2 third3 third4 second5 was a List", "first6 was a Str").Seq␤» 
## .map .flat has the same output as .flatmap 
# OUTPUT «("first1", "second5", "third3", "third4", "second2", "first6").Seq␤»