Documentation for routine invert

Documentation for routine invert, assembled from the following types:

class Any

From Any

(Any) method invert

Defined As:

    method invert(--> TODO)

TODO

role Baggy

From Baggy

(Baggy) method invert

Defined as:

method invert(Baggy:D: --> Seq:D)

Returns all elements and their respective weights as a Seq of Pairs, where the element itself is the value and the weight of that element is the key, i.e. the opposite of method pairs. Except for some esoteric cases invert on a Baggy type returns the same result as antipairs.

my $breakfast = bag <bacon eggs bacon>;
my $seq = $breakfast.invert;
say $seq.sort;                                    # OUTPUT: «(1 => eggs 2 => bacon)␤» 

class Map

From Map

(Map) method invert

Defined as:

method invert(Map:D: --> Seq:D)

Returns all keys and their respective values as a Seq of Pairs where the keys and values have been exchanged. The difference between invert and antipairs is that invert expands list values into multiple pairs.

my $m = Map.new('a' => (23), 'b' => 17);
say $m.invert;                                    # OUTPUT: «(2 => a 3 => a 17 => b)␤» 

class Pair

From Pair

(Pair) method invert

Defined as:

method invert(Pair:D:)

Returns a new Pair object with key and value exchanged, just like antipair, unless the value of the invocant does the Iterable role. In the latter case an Iterable structure of Pairs is returned since invert expands iterable values into multiple pairs.

my Pair $p1 = (6 => 'Perl');
say $p1.invert;                                   # OUTPUT: «Perl => 6␤» 
say $p1.invert.WHAT;                              # OUTPUT: «(Pair)␤» 
 
my Pair $p2 = ('Perl' => (56));
say $p2.invert;                                   # OUTPUT: «(5 => Perl 6 => Perl)␤» 
say $p2.invert.WHAT;                              # OUTPUT: «(List)␤» 
 
my Pair $p3 = ('Perl' => { cool => 'language'});
say $p3.invert;                                   # OUTPUT: «{cool => language => Perl}␤» 
say $p3.invert.WHAT;                              # OUTPUT: «(Hash)␤»