method invert

Documentation for method invert assembled from the following types:

class Any

From Any

(Any) method invert

Defined as:

method invert(--> List)

Returns an empty List.

say Any.invert# OUTPUT: «()␤» 

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 ='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: --> Seq:D)

Returns a Seq. If the .value of the invocant is NOT an Iterable, the Seq will contain a single Pair whose .key is the .value of the invocant and whose .value is the .key of the invocant:

:foo<bar>.invert.perl.say# OUTPUT: «(:bar("foo"),).Seq» 

If invocant's .value is an Iterable, the returned Seq will contain the same number of Pairs as items in the .value, with each of those items a .key of a pair and the .key of the invocant the .value of that pair:

:foo<Perl is great>.invert.perl.say;
# OUTPUT: «(:Perl("foo"), :is("foo"), :great("foo")).Seq» 
:foo{ :42a, :72}.invert.perl.say;
# OUTPUT: «((:a(42)) => "foo", (:b(72)) => "foo").Seq» 

To perform the exact .key and .value swap, use .antipair method.