X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FAttribute%2FNative%2FMethodProvider%2FArray.pm;h=b4a4971757134344b3ca1b99c9d01b6219c9bc1a;hb=b6cca0d5690feec99436fe952315d5d4feeb9473;hp=d98202b10a0396e27e166aa556a713334b6ac2b0;hpb=391c761c658ecef029f5326f0782962237e150af;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm b/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm index d98202b..b4a4971 100644 --- a/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm +++ b/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm @@ -1,7 +1,10 @@ package Moose::Meta::Attribute::Native::MethodProvider::Array; use Moose::Role; -our $VERSION = '0.89'; +use List::Util; +use List::MoreUtils; + +our $VERSION = '1.14'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -12,7 +15,7 @@ sub count : method { }; } -sub empty : method { +sub is_empty : method { my ( $attr, $reader, $writer ) = @_; return sub { scalar @{ $reader->( $_[0] ) } ? 0 : 1; @@ -23,10 +26,7 @@ sub first : method { my ( $attr, $reader, $writer ) = @_; return sub { my ( $instance, $predicate ) = @_; - foreach my $val ( @{ $reader->($instance) } ) { - return $val if $predicate->($val); - } - return; + List::Util::first { $predicate->() } @{ $reader->($instance) }; }; } @@ -34,7 +34,16 @@ sub map : method { my ( $attr, $reader, $writer ) = @_; return sub { my ( $instance, $f ) = @_; - CORE::map { $f->($_) } @{ $reader->($instance) }; + CORE::map { $f->() } @{ $reader->($instance) }; + }; +} + +sub reduce : method { + my ( $attr, $reader, $writer ) = @_; + return sub { + my ( $instance, $f ) = @_; + our ($a, $b); + List::Util::reduce { $f->($a, $b) } @{ $reader->($instance) }; }; } @@ -46,6 +55,13 @@ sub sort : method { if $predicate && ref $predicate ne 'CODE'; if ($predicate) { + # Although it would be nice if we could support just using $a and + # $b like sort already does, using $a or $b once in a package + # triggers the 'Name "main::a" used only once' warning, and there + # is no good way to avoid that, since it happens when the file + # which defines the coderef is compiled, before we even get a + # chance to see it here. So, we have no real choice but to use + # normal parameters. --doy CORE::sort { $predicate->( $a, $b ) } @{ $reader->($instance) }; } else { @@ -54,49 +70,43 @@ sub sort : method { }; } -sub grep : method { - my ( $attr, $reader, $writer ) = @_; - return sub { - my ( $instance, $predicate ) = @_; - CORE::grep { $predicate->($_) } @{ $reader->($instance) }; - }; -} - -sub elements : method { +sub shuffle : method { my ( $attr, $reader, $writer ) = @_; return sub { - my ($instance) = @_; - @{ $reader->($instance) }; + my ( $instance ) = @_; + List::Util::shuffle @{ $reader->($instance) }; }; } -sub join : method { +sub grep : method { my ( $attr, $reader, $writer ) = @_; return sub { - my ( $instance, $separator ) = @_; - join $separator, @{ $reader->($instance) }; + my ( $instance, $predicate ) = @_; + CORE::grep { $predicate->() } @{ $reader->($instance) }; }; } -sub head : method { +sub uniq : method { my ( $attr, $reader, $writer ) = @_; return sub { - $reader->( $_[0] )->[0]; + my ( $instance ) = @_; + List::MoreUtils::uniq @{ $reader->($instance) }; }; } -sub tail : method { +sub elements : method { my ( $attr, $reader, $writer ) = @_; return sub { - my $arr = $reader->( $_[0] ); - return @{ $arr }[1..$#{ $arr }]; + my ($instance) = @_; + @{ $reader->($instance) }; }; } -sub last : method { +sub join : method { my ( $attr, $reader, $writer ) = @_; return sub { - $reader->( $_[0] )->[-1]; + my ( $instance, $separator ) = @_; + join $separator, @{ $reader->($instance) }; }; } @@ -331,6 +341,21 @@ sub sort_in_place : method { }; } +sub natatime : method { + my ( $attr, $reader, $writer ) = @_; + return sub { + my ( $instance, $n, $f ) = @_; + my $it = List::MoreUtils::natatime($n, @{ $reader->($instance) }); + return $it unless $f; + + while (my @vals = $it->()) { + $f->(@vals); + } + + return; + }; +} + 1; __END__ @@ -339,12 +364,12 @@ __END__ =head1 NAME -Moose::Meta::Attribute::Native::MethodProvider::Array +Moose::Meta::Attribute::Native::MethodProvider::Array - role providing method generators for Array trait =head1 DESCRIPTION This is a role which provides the method generators for -L. Please check there for +L. Please check there for documentation on what methods are provided. =head1 METHODS @@ -357,9 +382,7 @@ documentation on what methods are provided. =head1 BUGS -All complex software has bugs lurking in it, and this module is no -exception. If you find a bug please either email me, or add the bug -to cpan-RT. +See L for details on reporting bugs. =head1 AUTHOR