line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
4
|
|
|
4
|
|
2713
|
use strict; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
119
|
|
2
|
4
|
|
|
4
|
|
20
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
187
|
|
3
|
|
|
|
|
|
|
package List::MapList 1.124; |
4
|
|
|
|
|
|
|
# ABSTRACT: map lists through a list of subs, not just one |
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
20
|
use Exporter 5.57 'import'; |
|
4
|
|
|
|
|
67
|
|
|
4
|
|
|
|
|
844
|
|
7
|
|
|
|
|
|
|
our @EXPORT = qw(mapcycle maplist); ## no critic |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
#pod =head1 SYNOPSIS |
10
|
|
|
|
|
|
|
#pod |
11
|
|
|
|
|
|
|
#pod Contrived heterogenous transform |
12
|
|
|
|
|
|
|
#pod |
13
|
|
|
|
|
|
|
#pod use List::MapList; |
14
|
|
|
|
|
|
|
#pod |
15
|
|
|
|
|
|
|
#pod my $code = [ |
16
|
|
|
|
|
|
|
#pod sub { $_ + 1 }, |
17
|
|
|
|
|
|
|
#pod sub { $_ + 2 }, |
18
|
|
|
|
|
|
|
#pod sub { $_ + 3 }, |
19
|
|
|
|
|
|
|
#pod sub { $_ + 4 } |
20
|
|
|
|
|
|
|
#pod ]; |
21
|
|
|
|
|
|
|
#pod |
22
|
|
|
|
|
|
|
#pod my @mapped_1 = maplist( $code, qw(1 2 3 4 5 6 7 8 9)); |
23
|
|
|
|
|
|
|
#pod # @mapped_1 is qw(2 4 6 8) |
24
|
|
|
|
|
|
|
#pod |
25
|
|
|
|
|
|
|
#pod my @mapped_2 = mapcycle( $code, qw(1 2 3 4 5 6 7 8 9)); |
26
|
|
|
|
|
|
|
#pod # @mapped_2 is qw(2 4 6 8 6 8 10 12 13) |
27
|
|
|
|
|
|
|
#pod |
28
|
|
|
|
|
|
|
#pod Ultra-secure partial rot13: |
29
|
|
|
|
|
|
|
#pod |
30
|
|
|
|
|
|
|
#pod my $rotsome = [ |
31
|
|
|
|
|
|
|
#pod sub { tr/a-zA-Z/n-za-mN-ZA-M/; $_ }, |
32
|
|
|
|
|
|
|
#pod sub { tr/a-zA-Z/n-za-mN-ZA-M/; $_ }, |
33
|
|
|
|
|
|
|
#pod sub { $_ }, |
34
|
|
|
|
|
|
|
#pod ]; |
35
|
|
|
|
|
|
|
#pod |
36
|
|
|
|
|
|
|
#pod my $plaintext = "Too many secrets."; |
37
|
|
|
|
|
|
|
#pod my $cyphertext = join '', mapcycle($rotsome, split //, $plaintext); |
38
|
|
|
|
|
|
|
#pod |
39
|
|
|
|
|
|
|
#pod =head1 DESCRIPTION |
40
|
|
|
|
|
|
|
#pod |
41
|
|
|
|
|
|
|
#pod List::MapList provides methods to map a list through a list of transformations, |
42
|
|
|
|
|
|
|
#pod instead of just one. The transformations are not chained together on each |
43
|
|
|
|
|
|
|
#pod element; only one is used, alternating sequentially. |
44
|
|
|
|
|
|
|
#pod |
45
|
|
|
|
|
|
|
#pod Here's a contrived example: given the transformations C<{ $_ = 0 }> and C<{ $_ |
46
|
|
|
|
|
|
|
#pod = 1 }>, the list C<(1, 2, 3, "Good morning", undef)> would become C<(0, 1, 0, 1, |
47
|
|
|
|
|
|
|
#pod 0)> or, without cycling, C<(0, 1)>.; |
48
|
|
|
|
|
|
|
#pod |
49
|
|
|
|
|
|
|
#pod (I use this code to process a part number in which each digit maps to a set of |
50
|
|
|
|
|
|
|
#pod product attributes.) |
51
|
|
|
|
|
|
|
#pod |
52
|
|
|
|
|
|
|
#pod =func maplist |
53
|
|
|
|
|
|
|
#pod |
54
|
|
|
|
|
|
|
#pod my @results = maplist(\@coderefs, LIST); |
55
|
|
|
|
|
|
|
#pod |
56
|
|
|
|
|
|
|
#pod This routine acts much like a normal C |
57
|
|
|
|
|
|
|
#pod references in C<$coderefs> in parallel with the list members. First first code |
58
|
|
|
|
|
|
|
#pod reference is used for the first list member, the next for the second, and so |
59
|
|
|
|
|
|
|
#pod on. Once the last code reference has been used, all further elements will be |
60
|
|
|
|
|
|
|
#pod mapped to C<()>. |
61
|
|
|
|
|
|
|
#pod |
62
|
|
|
|
|
|
|
#pod =cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub maplist { |
65
|
2
|
|
|
2
|
1
|
1149
|
my ($subs, $current) = (shift, 0); |
66
|
2
|
100
|
|
17
|
|
7
|
my $code = sub { $subs->[$current++] || sub { () }; }; |
|
17
|
|
|
|
|
57
|
|
|
9
|
|
|
|
|
23
|
|
67
|
2
|
|
|
|
|
6
|
map { $code->()->() } @_; |
|
17
|
|
|
|
|
42
|
|
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
#pod =func mapcycle |
71
|
|
|
|
|
|
|
#pod |
72
|
|
|
|
|
|
|
#pod my @results = mapcycle($coderefs, LIST); |
73
|
|
|
|
|
|
|
#pod |
74
|
|
|
|
|
|
|
#pod This routine is identical to C, but will cycle through the passed |
75
|
|
|
|
|
|
|
#pod coderefs over and over as needed. |
76
|
|
|
|
|
|
|
#pod |
77
|
|
|
|
|
|
|
#pod =cut |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub mapcycle { |
80
|
6
|
|
|
6
|
1
|
3850
|
my ($subs, $current) = (shift, 0); |
81
|
6
|
|
|
77
|
|
24
|
my $code = sub { $subs->[$current++ % @$subs]; }; |
|
77
|
|
|
|
|
146
|
|
82
|
6
|
|
|
|
|
14
|
map { $code->()->() } @_; |
|
77
|
|
|
|
|
211
|
|
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
__END__ |