Skip to content

Commit 2acde10

Browse files
committed
simplify code_ivs, which_id
1 parent c6c49e7 commit 2acde10

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

lib/PDL/Stats/Basic.pd

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,23 +1033,16 @@ Lookup specified var (obs) ids in $idv ($ido) (see B<rtable>) and return indices
10331033

10341034
sub which_id {
10351035
my ($id, $id_s) = @_;
1036-
1037-
my %ind;
1038-
@ind{ @$id } = ( 0 .. $#$id );
1039-
1040-
my @ind_select;
1041-
for (@$id_s) {
1042-
defined( $ind{$_} ) and push @ind_select, $ind{$_};
1043-
}
1044-
return pdl @ind_select;
1036+
my %ind; @ind{ @$id } = (0 .. $#$id);
1037+
pdl grep defined, map $ind{$_}, @$id_s;
10451038
}
10461039

1040+
my %code_bad = map +($_=>1), '', 'BAD';
10471041
sub code_ivs {
10481042
my ($var_ref) = @_;
10491043
$var_ref = [ $var_ref->list ] if UNIVERSAL::isa($var_ref, 'PDL');
1050-
my ($l, @filtered, %level) = (0,
1051-
map defined($_) && $_ ne '' && $_ ne 'BAD' ? $_ : undef, @$var_ref);
1052-
$level{$_} //= $l++ for grep defined, @filtered;
1044+
my @filtered = map !defined($_) || $code_bad{$_} ? undef : $_, @$var_ref;
1045+
my ($l, %level) = 0; $level{$_} //= $l++ for grep defined, @filtered;
10531046
my $pdl = pdl(map defined($_) ? $level{$_} : -1, @filtered)->setvaltobad(-1);
10541047
$pdl->check_badflag;
10551048
wantarray ? ($pdl, \%level) : $pdl;

0 commit comments

Comments
 (0)