package Moose::Meta::Attribute::Native::MethodProvider::Array;
use Moose::Role;
-our $VERSION = '0.87';
+use List::Util;
+use List::MoreUtils;
+
+our $VERSION = '0.92';
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
};
}
-sub empty : method {
+sub is_empty : method {
my ( $attr, $reader, $writer ) = @_;
return sub {
- scalar @{ $reader->( $_[0] ) } ? 1 : 0;
+ scalar @{ $reader->( $_[0] ) } ? 0 : 1;
};
}
-sub find : method {
+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) });
};
}
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) };
};
}
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 {
};
}
-sub grep : method {
+sub shuffle : method {
my ( $attr, $reader, $writer ) = @_;
return sub {
- my ( $instance, $predicate ) = @_;
- CORE::grep { $predicate->($_) } @{ $reader->($instance) };
+ my ( $instance ) = @_;
+ List::Util::shuffle @{ $reader->($instance) };
};
}
-sub elements : method {
+sub grep : method {
my ( $attr, $reader, $writer ) = @_;
return sub {
- my ($instance) = @_;
- @{ $reader->($instance) };
+ my ( $instance, $predicate ) = @_;
+ CORE::grep { $predicate->() } @{ $reader->($instance) };
};
}
-sub join : method {
+sub uniq : method {
my ( $attr, $reader, $writer ) = @_;
return sub {
- my ( $instance, $separator ) = @_;
- join $separator, @{ $reader->($instance) };
+ my ( $instance ) = @_;
+ List::MoreUtils::uniq @{ $reader->($instance) };
};
}
-sub first : method {
+sub elements : method {
my ( $attr, $reader, $writer ) = @_;
return sub {
- $reader->( $_[0] )->[0];
+ 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) };
};
}
};
}
+sub natatime : method {
+ my ( $attr, $reader, $writer ) = @_;
+ return sub {
+ my ( $instance, $n, $f ) = @_;
+ my $it = List::MoreUtils::natatime($n, @{ $reader->($instance) });
+ if ($f) {
+ while (my @vals = $it->()) {
+ $f->(@vals);
+ }
+ }
+ $it;
+ };
+}
+
1;
__END__
Moose::Meta::Attribute::Native::MethodProvider::Array
-=head1 SYNOPSIS
-
- package Stuff;
- use Moose;
- use Moose::AttributeHelpers;
-
- has 'options' => (
- traits => ['Array'],
- is => 'rw',
- isa => 'ArrayRef[Str]',
- default => sub { [] },
- auto_deref => 1,
- handles => {
- all_options => 'elements',
- map_options => 'map',
- filter_options => 'grep',
- find_option => 'find',
- first_option => 'first',
- last_option => 'last',
- get_option => 'get',
- join_options => 'join',
- count_options => 'count',
- do_i_have_options => 'empty',
- sorted_options => 'sort',
- }
- );
-
- no Moose;
- 1;
-
=head1 DESCRIPTION
This is a role which provides the method generators for
-L<Moose::Meta::Attribute::Trait::Native::Array>. Please check there for documentation on what methods are provided.
+L<Moose::Meta::Attribute::Native::Trait::Array>. Please check there for
+documentation on what methods are provided.
=head1 METHODS