routine pairs

Documentation for routine pairs assembled from the following types:

role Baggy

From Baggy

(Baggy) method pairs

Defined as:

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

Returns all elements and their respective weights as a Seq of Pairs where the key is the element itself and the value is the weight of that element.

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

class Any

From Any

(Any) method pairs

Defined as:

multi method pairs(Any:U:)
multi method pairs(Any:D:)

Returns an empty List if the invocant is a type object:

say Num.pairs# OUTPUT: «()␤»

For a value object, it converts the invocant to a List via the list method and returns the result of List.pairs on it.

<1 2 2 3 3 3>.Bag.pairs.say;# OUTPUT: «(1 => 1 3 => 3 2 => 2)␤»

In this case, every element (with weight) in a bag is converted to a pair.

class Map

From Map

(Map) method pairs

Defined as:

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

Returns a Seq of all pairs in the Map.

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

class Pair

From Pair

(Pair) method pairs

Defined as:

multi method pairs(Pair:D:)

Returns a list of one Pair, namely this one.

my $p = (Perl => 6);
say $p.pairs.^name# OUTPUT: «List␤» 
say $p.pairs[0];    # OUTPUT: «Perl => 6␤»

class Capture

From Capture

(Capture) method pairs

Defined as:

multi method pairs(Capture:D: --> Seq:D)

Returns all arguments, the positional followed by the named, as a Seq of Pairs. Positional arguments have their respective ordinal value, starting at zero, as key while the named arguments have their names as key.

my Capture $c = \(23apples => (red => 2));
say $c.pairs;                                     # OUTPUT: «(0 => 2 1 => 3 apples => red => 2)␤»

class List

From List

(List) routine pairs

Defined as:

sub    pairs($list --> Seq:D)
method pairs(List:D: --> Seq:D)

Returns a sequence of pairs, with the indexes as keys and the list values as values.

<a b c>.pairs   # (0 => a 1 => b 2 => c)