|
| 1 | +=begin pod |
| 2 | +
|
| 3 | +=head1 Pair |
| 4 | +
|
| 5 | +Consists of two parts, a I<key> and a I<value>. C<Pair>s can be seen as the |
| 6 | +atomic units in C<Hash>es, and they are also used in conjunction with named |
| 7 | +arguments and parameters. |
| 8 | +
|
| 9 | +There are three syntaxes for C<Pair>s: |
| 10 | +
|
| 11 | + 'key' => 'value' # this... |
| 12 | + :key<value> # ...means the same as this |
| 13 | + :$foo # short for foo => $foo |
| 14 | +
|
| 15 | +=head2 Methods |
| 16 | +
|
| 17 | +=head3 key |
| 18 | +
|
| 19 | + multi method key(Pair:D:) |
| 20 | +
|
| 21 | +Gives the I<key> part of the C<Pair>. |
| 22 | +
|
| 23 | +=head3 value |
| 24 | +
|
| 25 | + multi method value(Pair:D:) |
| 26 | +
|
| 27 | +Gives the I<value> part of the C<Pair>. |
| 28 | +
|
| 29 | +=head3 cmp |
| 30 | +
|
| 31 | + multi sub infix:<cmp>(Pair:D, Pair:D) |
| 32 | +
|
| 33 | +The type-agnostic comparator; compares two C<Pair>s. Compares first their |
| 34 | +I<key> parts, and then (if the first comparison yielded 0) the I<value> parts. |
| 35 | +
|
| 36 | +=head3 fmt |
| 37 | +
|
| 38 | + multi method fmt(Pair:D:) returns Str:D |
| 39 | +
|
| 40 | +Takes a I<format string>, and returns a string the I<key> and I<value> |
| 41 | +parts of the C<Pair> formatted. Here's an example: |
| 42 | +
|
| 43 | + my $pair = :Earth(1); |
| 44 | + say $pair.fmt("%s is %.3f AU away from the sun") |
| 45 | + # Prints "Earth is 1.000 AU away from the sun" |
| 46 | +
|
| 47 | +For more about format strings, see X<sprintf>. |
| 48 | +
|
| 49 | +=head3 kv |
| 50 | +
|
| 51 | + multi method kv(Pair:D:) returns List:D |
| 52 | +
|
| 53 | +Returns a two-element C<List> with the I<key> and I<value> parts of the |
| 54 | +C<Pair>, in that order. This method is a special case of the same-named |
| 55 | +method on C<Hash>, which returns all its entries as a list of keys and |
| 56 | +values. |
| 57 | +
|
| 58 | +=head3 pairs |
| 59 | +
|
| 60 | + multi method pairs(Pair:D:) |
| 61 | +
|
| 62 | +Returns a list of one C<Pair>, namely this one. |
| 63 | +
|
| 64 | +=end pod |
0 commit comments