Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: nested maps

by LanX (Saint)
on Sep 21, 2020 at 11:37 UTC ( [id://11121996]=note: print w/replies, xml ) Need Help??


in reply to Re^3: nested maps
in thread nested maps

OK, my prototype wasn't totally correct.

Here an implementation allowing to combine multiple cross(), like in the OP

use strict; use warnings; use Data::Dump qw/pp dd/; use 5.12.0; # https://perlmonks.org/?node_id=11121968 sub cross(&\@@) { my ( $code, $a_a, @b ) = @_; #warn pp $code, $a_a,\@b; map { $a = $_; map { $b=$_; &$code } @b; } @$a_a; } my @a = 1..3; my @b = 4..6; my @c = 7..9; pp cross { $a.$b } @a, cross { $a.$b } @b,@c;

( 147, 148, 149, 157, 158, 159, 167, 168, 169, 247, 248, 249, 257, 258, 259, 267, 268, 269, 347, 348, 349, 357, 358, 359, 367, 368, 369, )

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^5: nested maps
by tobyink (Canon) on Sep 21, 2020 at 13:18 UTC

    I've got something on CPAN which does similar.

    use strict; use warnings; use List::MapMulti; use Data::Dump 'pp'; my @a = 1..3; my @b = 4..6; my @c = 7..9; pp mapm { $_[0] . $_[1] . $_[2] } \@a, \@b, \@c;

    It can also be used as an iterator:

    use strict; use warnings; use List::MapMulti 'iterator_multi'; use feature 'say'; my @a = 1..3; my @b = 4..6; my @c = 7..9; my $iter = iterator_multi( \@a, \@b, \@c ); while ( my ( $a, $b, $c ) = $iter->() ) { say $a, $b, $c; }

      Thank you for the replies, and I'm sorry my query wasn't clear enough. I am not looking for additional solutions. I am looking for a way to choose between two of the solutions already presented.

      Specifically, in an application that uses a nested map only once, is it better to use a single temporary variable as in the example quoted in DrHyde's post, or is it better to load another CPAN module that only gets used the one time, simply to avoid this temporary variable? And is "better" different for the Perl interpreter than it is for the human who has to understand/maintain the code?

      If you want to offer another approach and explain why it's better than both those options, that's also useful.

      But what I'm interested in is the decision process, not new coding techniques. Thank you, and I hope that clarifies.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11121996]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found