Changeset 21350

Show
Ignore:
Timestamp:
10/15/08 11:51:42 (5 years ago)
Author:
yappo
Message:

merge from List::RubyLike? (http://github.com/naoya/list-rubylike/tree/master)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/List-Rubyish/trunk/lib/List/Rubyish.pm

    r21322 r21350  
    22use strict; 
    33use warnings; 
     4use overload 
     5    '<<'     => 'push', 
     6    '+'      => 'add', 
     7    fallback => 1; 
     8 
    49our $VERSION = '0.01'; 
    510 
     
    1015sub new { 
    1116    my ($class, $array) = @_; 
    12     $class = ref $class || $class; 
     17    $class = ref $class if ref $class; 
    1318    $array ||= []; 
    1419    croak sprintf("Argument must be an array reference (%s)", ref $array) 
     
    4752sub slice { 
    4853    my $self = CORE::shift; 
    49     my ($s, $e) = @_; 
     54    my ($start, $end) = @_; 
    5055    my $last = $#{$self}; 
    51     # warn "s: $s, e: $e, last: $last"; 
    52     if (defined $e) { 
    53         if ($s == 0 && $last <= $e) { 
     56    if (defined $end) { 
     57        if ($start == 0 && $last <= $end) { 
    5458            return $self; 
    5559        } else { 
    56             $e = $last if ($last < $e); 
    57             return $self->new([ @$self[ $s .. $e ] ]); 
     60            $end = $last if $last < $end; 
     61            return $self->new([ @$self[ $start .. $end ] ]); 
    5862        } 
    59     } elsif (defined $s && 0 < $s && $last <= $s) { 
    60         # warn $self->first . "s: $s, e: $e, self:" . $#{$self}; 
     63    } elsif (defined $start && 0 < $start && $last <= $start) { 
    6164        return $self->new([]); 
    6265    } else { 
     
    203206 
    204207sub find { 
    205     my ($self, $code) = @_; 
    206     croak "Argument must be a code" unless ref $code eq 'CODE'; 
     208    my ($self, $cond) = @_; 
     209    my $code = (ref $cond and ref $cond eq 'CODE') 
     210        ? $cond 
     211        : sub { $_ eq $cond }; 
     212 
    207213    for (@$self) { &$code and return $_ } 
     214    return; 
    208215} 
    209216 
     
    211218    my ($self, $target) = @_; 
    212219    my $code = (ref $target eq 'CODE') ? $target : sub { CORE::shift eq $target }; 
     220 
    213221    for (my $i = 0; $i < $self->length; $i++) { 
    214         &$code($self->[$i]) and return $i; 
     222        $code->($self->[$i]) and return $i; 
    215223    } 
     224    return; 
    216225} 
    217226