Revision f30b6f52
Von Sven Schöling vor mehr als 15 Jahren hinzugefügt
SL/MoreCommon.pm | ||
---|---|---|
3 | 3 |
require Exporter; |
4 | 4 |
@ISA = qw(Exporter); |
5 | 5 |
|
6 |
@EXPORT = qw(save_form restore_form compare_numbers any); |
|
6 |
@EXPORT = qw(save_form restore_form compare_numbers any cross);
|
|
7 | 7 |
|
8 | 8 |
use YAML; |
9 | 9 |
|
... | ... | |
85 | 85 |
return 0; |
86 | 86 |
} |
87 | 87 |
|
88 |
=item cross BLOCK ARRAY ARRAY |
|
89 |
|
|
90 |
Evaluates BLOCK for each combination of elements in ARRAY1 and ARRAY2 |
|
91 |
and returns a new list consisting of BLOCK's return values. |
|
92 |
The two elements are set to $a and $b. |
|
93 |
Note that those two are aliases to the original value so changing them |
|
94 |
will modify the input arrays. |
|
95 |
|
|
96 |
# append each to each |
|
97 |
@a = qw/a b c/; |
|
98 |
@b = qw/1 2 3/; |
|
99 |
@x = pairwise { "$a$b" } @a, @b; |
|
100 |
# returns a1, a2, a3, b1, b2, b3, c1, c2, c3 |
|
101 |
|
|
102 |
As cross expects an array but returns a list it is not directly chainable |
|
103 |
at the moment. This will be corrected in the future. |
|
104 |
|
|
105 |
=cut |
|
106 |
sub cross(&\@\@) { |
|
107 |
my $op = shift; |
|
108 |
use vars qw/@A @B/; |
|
109 |
local (*A, *B) = @_; # syms for caller's input arrays |
|
110 |
|
|
111 |
# Localise $a, $b |
|
112 |
my ($caller_a, $caller_b) = do { |
|
113 |
my $pkg = caller(); |
|
114 |
no strict 'refs'; |
|
115 |
\*{$pkg.'::a'}, \*{$pkg.'::b'}; |
|
116 |
}; |
|
117 |
|
|
118 |
local(*$caller_a, *$caller_b); |
|
119 |
|
|
120 |
# This map expression is also the return value. |
|
121 |
map { my $a_index = $_; |
|
122 |
map { my $b_index = $_; |
|
123 |
# assign to $a, $b as refs to caller's array elements |
|
124 |
(*$caller_a, *$caller_b) = \($A[$a_index], $B[$b_index]); |
|
125 |
$op->(); # perform the transformation |
|
126 |
} 0 .. $#B; |
|
127 |
} 0 .. $#A; |
|
128 |
} |
|
129 |
|
|
88 | 130 |
1; |
bin/mozilla/common.pl | ||
---|---|---|
580 | 580 |
$lxdebug->leave_sub(); |
581 | 581 |
} |
582 | 582 |
|
583 |
=item cross BLOCK ARRAY ARRAY |
|
584 |
|
|
585 |
Evaluates BLOCK for each combination of elements in ARRAY1 and ARRAY2 |
|
586 |
and returns a new list consisting of BLOCK's return values. |
|
587 |
The two elements are set to $a and $b. |
|
588 |
Note that those two are aliases to the original value so changing them |
|
589 |
will modify the input arrays. |
|
590 |
|
|
591 |
# append each to each |
|
592 |
@a = qw/a b c/; |
|
593 |
@b = qw/1 2 3/; |
|
594 |
@x = pairwise { "$a$b" } @a, @b; |
|
595 |
# returns a1, a2, a3, b1, b2, b3, c1, c2, c3 |
|
596 |
|
|
597 |
As cross expects an array but returns a list it is not directly chainable |
|
598 |
at the moment. This will be corrected in the future. |
|
599 |
|
|
600 |
=cut |
|
601 |
sub cross(&\@\@) { |
|
602 |
my $op = shift; |
|
603 |
use vars qw/@A @B/; |
|
604 |
local (*A, *B) = @_; # syms for caller's input arrays |
|
605 |
|
|
606 |
# Localise $a, $b |
|
607 |
my ($caller_a, $caller_b) = do { |
|
608 |
my $pkg = caller(); |
|
609 |
no strict 'refs'; |
|
610 |
\*{$pkg.'::a'}, \*{$pkg.'::b'}; |
|
611 |
}; |
|
612 |
|
|
613 |
local(*$caller_a, *$caller_b); |
|
614 |
|
|
615 |
# This map expression is also the return value. |
|
616 |
map { my $a_index = $_; |
|
617 |
map { my $b_index = $_; |
|
618 |
# assign to $a, $b as refs to caller's array elements |
|
619 |
(*$caller_a, *$caller_b) = \($A[$a_index], $B[$b_index]); |
|
620 |
$op->(); # perform the transformation |
|
621 |
} 0 .. $#B; |
|
622 |
} 0 .. $#A; |
|
623 |
} |
|
624 |
|
|
625 | 583 |
1; |
Auch abrufbar als: Unified diff
cross von common.pl nach MoreCommon verschoben.