in reply to Re: RFC: Idiomless Perl
in thread RFC: Idiomless Perl
Just to play Devil's Advocate:
What benefit stands to be gained to using $arrayobj->find($key) as opposed to mapping the list to a hash and testing a key?
It's not hard to imagine an implementation of $array->contains($key) (instead of "find", which I would hate) using grep or List::MoreUtil's indexes (which is XS, and therefore pretty fast). Examples:
## discover if an array contains a value, using grep sub contains { ## usage: $bool = $array->contains( $value ) # is $value in $array +obj? ? my ($self, $val) = @_; my $r_array = $self->{data}; # assume this contains the array ## for example, assume values are strings my $cnt = grep { $_ eq $value } @$r_array; return $cnt ? 1 : 0; }
## discover if an array contains a value, using List::Util::indexes; sub contains { ## usage: $bool = $array->contains( $value ) # is $value in $array +obj? ? my ($self, $val) = @_; my $r_array = $self->{data}; # assume this contains the array ## for example, assume values are strings my $cnt = List::Util::indexes { $_ eq $value } @$r_array; return $cnt ? 1 : 0; }
This latter has the advantage that it's easily modifiable to return a list of indexes that match (since that's what indexes does already)
Using map to cast an array to a hash and testing keys is only useful if you'll be checking for the existence of several values in an array that won't change between your tests. So, I think the real question should be what value does this wrapper provide over using Perl's built-in capabilities?
I think the OP addressed that just fine -- it's syntactic sugar for those more familiar with OO-based languages who are trying to learn Perl, or who want to consistently use OO for everything for some reason.
|
|---|