doc icon indicating copy to clipboard operation
doc copied to clipboard

Need a simple example of inverting @a to a Hash by reversing its keys and values

Open tbrowder opened this issue 10 months ago • 3 comments

tbrowder avatar Mar 01 '25 19:03 tbrowder

I suggest breaking this existing doc verbiage (from https://docs.raku.org/type/List#routine_invert) where I've inserted a â–²:

Assumes every element of the List is a L<C<Pair>|/type/Pair>. Returns all elements as a L<C<Seq>|/type/Seq> of L<C<Pair>|/type/Pair>s where the keys and values have been exchanged. â–² If the value of a L<C<Pair>|/type/Pair> is an L<C<Iterable>|/type/Iterable>, then it will expand the values of that L<C<Iterable>|/type/Iterable> into separate pairs.

by inserting this code:

my @a = <list of four strings>;
say @a.pairs.invert.hash; # {list => 0, four => 2, of => 1, strings => 3}

Or perhaps instead add:

my @a = <list of four strings>;
say @a.antipairs.hash; # {list => 0, four => 2, of => 1, strings => 3}

to the antipairs doc?

raiph avatar Mar 02 '25 00:03 raiph

On Sat, Mar 1, 2025 at 18:10 raiph @.***> wrote:

I suggest breaking this existing doc verbiage (from https://docs.raku.org/type/List#routine_invert) where I've inserted a â–²:

Assumes every element of the List is a L<C|/type/Pair>. Returns all elements as a L<C|/type/Seq> of L<C|/type/Pair>s where the keys and values have been exchanged. â–² If the value of a L<C|/type/Pair> is an L<C|/type/Iterable>, then it will expand the values of that L<C|/type/Iterable> into separate pairs.

by inserting this code:

my @a = ; say @a.pairs.invert.hash; # {array => 0, four => 2, of => 1, strings => 3}

If you're OK with that I'll do a doc PR/commit for it.

— Reply to this email directly, view it on GitHub https://github.com/Raku/doc/issues/4561#issuecomment-2692480566, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH5VVGHSAAXYYKV2I7DIGD2SJD63AVCNFSM6AAAAABYEL6PCGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMOJSGQ4DANJWGY . You are receiving this because you authored the thread.Message ID: @.***> [image: raiph]raiph left a comment (Raku/doc#4561) https://github.com/Raku/doc/issues/4561#issuecomment-2692480566

I suggest breaking this existing doc verbiage (from https://docs.raku.org/type/List#routine_invert) where I've inserted a â–²:

That would be very useful, thank you!

tbrowder avatar Mar 02 '25 02:03 tbrowder

I think the .antipairs method alone is the best way to show the way. Maybe just improve its indexing in the docs.

tbrowder avatar Mar 02 '25 12:03 tbrowder