Revision 26135

Date:
2009/04/08 19:21:21
Author:
lwall
Revision Log:
[S32/Containers] make it possible to reverse a hash without (too much) loss of
information when there are duplicate values or list values
Files:

Legend:

 
Added
 
Removed
 
Modified
  • docs/Perl6/Spec/S32-setting-library/Containers.pod

     
    15 15 Moritz Lenz <moritz@faui2k3.org>
    16 16 Tim Nelson <wayland@wayland.id.au>
    17 17 Date: 19 Feb 2009 extracted from S29-functions.pod
    18 Last Modified: 18 Mar 2009
    19 Version: 3
    18 Last Modified: 8 Apr 2009
    19 Version: 4
    20 20
    21 21 The document is a draft.
    22 22
     
    28 28
    29 29 =head2 Positional
    30 30
    31 role Positional {...}
    31 role Positional {...}
    32 32
    33 33 The C<Positional> role implies the ability to support C<< postcircumfix:<[ ]> >>.
    34 34
    35 35 =head2 Associative
    36 36
    37 role Associative {...}
    37 role Associative {...}
    38 38
    39 39 The C<Associative> role implies the ability to support C<< postcircumfix:<{ }> >>.
    40 40
    41 41 =head2 Container
    42 42
    43 role Container {...}
    43 role Container {...}
    44 44
    45 45 =over
    46 46
     
    112 112
    113 113 The following are defined in the C<List> role/class:
    114 114
    115 role List does Container does Positional {...}
    115 role List does Container does Positional {...}
    116 116
    117 117 =over
    118 118
     
    267 267
    268 268 =item reverse
    269 269
    270 role Hash {
    271 our Hash multi method reverse ( %hash: ) is export {
    272 (my %result){%hash.values} = %hash.keys;
    273 %result;
    274 }
    275 }
    276
    277 270 our List multi method reverse ( @values: ) is export
    278 271 our List multi reverse ( *@values ) {
    279 272 gather {
     
    404 397
    405 398 All these methods are defined in the C<Array> role/class.
    406 399
    407 role Array does List {...}
    400 role Array does List {...}
    408 401
    409 402 =over
    410 403
     
    554 547
    555 548 The following are defined in the C<Hash> role.
    556 549
    557 role Hash does Container does Associative {...}
    550 role Hash does Container does Associative {...}
    558 551
    559 552 =over 4
    560 553
     
    643 636 Returns a junction which will only match against another value if none of
    644 637 the keys in the hash matches.
    645 638
    639 =item reverse
    640
    641 our List multi method reverse ( %hash: ) is export {
    642 map -> $k, $v { $v X=> $k }, %hash.kv;
    643 }
    644
    645 Produces a backmapping of values to keys, expanding list values
    646 into multiple pairs. (The C<< X=> >> expands C<$v> if it is a list.)
    647 [NB: this may need refinement to handle keys that do C<Positional>.]
    648
    649 =item push
    650
    651 our Int multi method push ( @hash: *@values ) is export
    652
    653 Like hash assignment insofar as it accepts either C<Pair> objects or
    654 alternating keys and values; however, unlike assignment, when
    655 a duplicate key is detected, coerces the colliding entry's value to an
    656 array and pushes the Pair's value onto that array. Hence to reverse
    657 a hash containing duplicate values without losing information, say:
    658
    659 %out.push(%in.reverse)
    660
    661 Note that when reading the values of such a hash, you must not assume
    662 that all the elements are arrays, since the first instance of a key
    663 merely sets the value without turning it into an array. (Fortunately,
    664 most list operators create a list of one element when used on an object
    665 that is not a list.)
    666
    667 The intention is that
    668
    669 %bar.push(%foo.reverse);
    670 %baz.push(%bar.reverse);
    671
    646 672 =back
    647 673
    648 674 =head2 Tieable
    649 675
    650 role Tieable {...}
    676 role Tieable {...}
    651 677
    652 678 =head1 Classes
    653 679
     
    658 684
    659 685 =head2 Seq
    660 686
    661 class Seq does Positional {...}
    687 class Seq does Positional {...}
    662 688
    663 689 =head2 Range
    664 690
    665 class Range does Positional {
    691 class Range does Positional {
    666 692 method from() {...}
    667 693 method to() {...}
    668 694 method min() {...}
    669 695 method max() {...}
    670 696 method List minmax() {...}
    671 }
    697 }
    672 698
    673 699 =head2 Buf
    674 700
    675 class Buf does Positional {...}
    701 class Buf does Positional {...}
    676 702
    677 703 =head2 Pair
    678 704
    679 class Pair does Associative {...}
    705 class Pair does Associative {...}
    680 706
    707 =item reverse
    708
    709 our List multi method reverse ( $pair: ) is export {
    710 $pair.value X=> $pair.key
    711 }
    712
    681 713 =head2 Mapping
    682 714
    683 class Mapping does Associative {...}
    715 class Mapping does Associative {...}
    684 716
    685 717 =head2 Set
    686 718
    687 class Set does Associative {...}
    719 class Set does Associative {...}
    688 720
    689 721 =head2 Bag
    690 722
    691 class Bag does Associative {...}
    723 class Bag does Associative {...}
    692 724
    693 725 =head2 KeyHash
    694 726
    695 class KeyHash does Associative {...}
    727 class KeyHash does Associative {...}
    696 728
    697 729 =head2 junction
    698 730