in reply to RFC: Idiomless Perl

Do you have an easy way to chain maps and sorts and return an OO::Array in mind? Would someone using your framework be able to port older code containing such idioms with little effort?

Would it be worth it to add this wrapping layer? What benefit stands to be gained to using $arrayobj->find($key) as opposed to mapping the list to a hash and testing a key?

As TedYoung said in his earlier comment: Re: RFC: Idiomless Perl these things are core to perl and are difficult or pointless to wrap.

Replies are listed 'Best First'.
Re^2: RFC: Idiomless Perl
by radiantmatrix (Parson) on Feb 05, 2007 at 20:30 UTC

    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.

    <radiant.matrix>
    Ramblings and references
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet