routine invert
| 1 | class List |
| 1.1 | (List) routine invert |
| 2 | class Any |
| 2.1 | (Any) method invert |
| 3 | class HyperSeq |
| 3.1 | (HyperSeq) method invert |
| 4 | role Baggy |
| 4.1 | (Baggy) method invert |
| 5 | class RaceSeq |
| 5.1 | (RaceSeq) method invert |
| 6 | class Pair |
| 6.1 | (Pair) method invert |
| 7 | class Map |
| 7.1 | (Map) method invert |
Documentation for routine invert assembled from the following types:
class List
From List
(List) routine invert
Defined as:
method invert(List: --> Seq)
Assumes every element of the List is a Pair. Returns all elements as a Seq of Pairs where the keys and values have been exchanged. If the value of a Pair is an Iterable, then it will expand the values of that Iterable into separate pairs.
my = List.new('a' => (2, 3), 'b' => 17);say .invert; # OUTPUT: «(2 => a 3 => a 17 => b)»
class Any
From Any
(Any) method invert
Defined as:
multi method invert(Any:)multi method invert(Any:)
Applied to a type object will return an empty list; applied to an object will convert it to a list and apply List.invert to it, that is, interchange key with value in every Pair. The resulting list needs to be a list of Pairs.
"aaabbcccc".comb.Bag.invert.say; # OUTPUT: «(4 => c 3 => a 2 => b)»
In this case, a Bag can be converted to a list of Pairs. If the result of converting the object to a list is not a list of pairs, the method will fail.
class HyperSeq
From HyperSeq
(HyperSeq) method invert
method invert(HyperSeq:)
Inverts the HyperSeq created from a Seq by .hyper.
role Baggy
From Baggy
(Baggy) method invert
Defined as:
method invert(Baggy: --> Seq)
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 = bag <bacon eggs bacon>;my = .invert;say .sort; # OUTPUT: «(1 => eggs 2 => bacon)»
class RaceSeq
From RaceSeq
(RaceSeq) method invert
method invert(RaceSeq:)
Inverts the RaceSeq created from a Seq by .race.
class Pair
From Pair
(Pair) method invert
Defined as:
method invert(Pair: --> Seq)
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.invert.perl.say;# OUTPUT: «((:a(42)) => "foo", (:b(72)) => "foo").Seq»
To perform the exact .key and .value swap, use .antipair method.
class Map
From Map
(Map) method invert
Defined as:
method invert(Map: --> Seq)
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 = Map.new('a' => (2, 3), 'b' => 17);say .invert; # OUTPUT: «(2 => a 3 => a 17 => b)»