Skip to content

Commit 86d7dd5

Browse files
committed
document .pairup and List.invert
1 parent 4693323 commit 86d7dd5

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

S32-setting-library/Containers.pod

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ DRAFT: Synopsis 32: Setting Library - Containers.pod
88

99
Created: 19 Feb 2009 extracted from S29-functions.pod
1010

11-
Last Modified: 26 Oct 2014
12-
Version: 48
11+
Last Modified: 26 Feb 2015
12+
Version: 49
1313

1414
=head1 Function Roles
1515

@@ -681,6 +681,38 @@ are converted to a list of individual values before permuting them.)
681681

682682
The functional form assumes it is working on a list of integers C<^$n>.
683683

684+
=item pairup
685+
686+
multi method pairup ( )
687+
688+
Turns a list of mixed keys, values, and pairs into a list of pairs in
689+
the same way that a hash composer does, only without creating the hash.
690+
While there is more list to process, the next value is examined. If it
691+
is a Pair, it is passed through unchanged. Otherwise a second value is
692+
also taken from the list and those two values together are turned into
693+
a pair. It is an X::Pairup::OddNumber error if there is no next value.
694+
(It is not an error if the second value happens be a pair; if so, it
695+
merely becomes the value paired with the key of the first value.)
696+
697+
Obviously you cannot create pairs where the keys are Pairs using this
698+
function. Use a map or some such for that.
699+
700+
=item invert
701+
702+
multi method invert ( --> List ) is export {
703+
self.map: -> $p { $.value X=> $.key }
704+
}
705+
706+
Produces a backmapping of values to keys, expanding list values
707+
into multiple pairs. (The C<< X=> >> expands C<$v> if it is a list.)
708+
To handle keys that do C<Positional> you'll need to write your own map.
709+
710+
The invocant must be a list containing only Pairs, and throws an error if
711+
anything that is not a Pair is passed. To get combine kv values, use
712+
C<.pairup> first:
713+
714+
@kv.pairup.invert
715+
684716
=back
685717

686718
=head2 Array
@@ -1013,7 +1045,7 @@ the keys in the hash matches.
10131045

10141046
Produces a backmapping of values to keys, expanding list values
10151047
into multiple pairs. (The C<< X=> >> expands C<$v> if it is a list.)
1016-
[NB: this may need refinement to handle keys that do C<Positional>.]
1048+
To handle keys that do C<Positional> you'll need to write your own map.
10171049

10181050
=item push
10191051

0 commit comments

Comments
 (0)