Skip to content

Commit

Permalink
added regular expression support to first method in Mojo::Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 3, 2012
1 parent 21b9832 commit 40aa2e1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

3.02 2012-07-03
- Added regular expression support to grep method in Mojo::Collection.
- Added regular expression support to first and grep methods in
Mojo::Collection.
- Improved documentation.
- Improved tests.
- Fixed JSON Pointer escaping.
Expand Down
8 changes: 5 additions & 3 deletions lib/Mojo/Collection.pm
Expand Up @@ -28,7 +28,7 @@ sub each {
sub first {
my ($self, $cb) = @_;
return $self->[0] unless $cb;
return List::Util::first { $_->$cb } @$self;
return List::Util::first { $_ ~~ $cb } @$self;

This comment has been minimized.

Copy link
@coldfire-x

coldfire-x Jul 4, 2012

use 5.10, smart match

}

# "All right, let's not panic.
Expand Down Expand Up @@ -132,10 +132,12 @@ Evaluate closure for each element in collection.
=head2 C<first>
my $first = $collection->first;
my $first = $collection->first(qr/foo/);
my $first = $collection->first(sub {...});
Evaluate closure for each element in collection and return the first one for
which the closure returns true.
Evaluate regular expression or closure for each element in collection and
return the first one that matched the regular expression, or for which the
closure returned true.
my $five = $collection->first(sub { $_ == 5 });
Expand Down
3 changes: 2 additions & 1 deletion t/mojo/collection.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 52;
use Test::More tests => 53;

# "'What are you lookin at?' - the innocent words of a drunken child."
use Mojo::Collection 'c';
Expand Down Expand Up @@ -29,6 +29,7 @@ is $collection->first, 5, 'right result';
is_deeply $collection->first(sub { ref $_ eq 'ARRAY' }), [3, 2],
'right result';
is $collection->first(sub { shift() < 5 }), 4, 'right result';
is $collection->first(qr/[1-4]/), 4, 'right result';
is $collection->first(sub { ref $_ eq 'CODE' }), undef, 'no result';
$collection = c();
is $collection->first, undef, 'no result';
Expand Down

0 comments on commit 40aa2e1

Please sign in to comment.