If you're going to replicate it, can you at least make it idiomatically perl? ;-) You have the desired test value in $_ already (good!), but then you go and ruin it by passing it in. Why not just:
sub partition (&@) {
my ($condition, @array) = @_;
my (@true, @false);
foreach (@array) {
if ( $condition->() ) {
push @true, $_;
}
else {
push @false, $_;
}
}
return \@true, \@false;
}
my ($ok, $no) = partition { $_->name =~ m/[0-9]{2}$/ } @{ $rec->vals }
+;
In what is likely to be a common case, where the array is an array of strings rather than object references, that partition block becomes even more trivial. I've taken to this idiom in the last few months just because of the way it simplifies the callbacks. That it is probably faster (no parameters to pass around) actually has had no bearing on whether I use this or not (may shave off a second or two over three hours - not a concern). |