Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added experimental slice method to Mojo::Collection
  • Loading branch information
kraih committed Oct 12, 2011
1 parent 569ea90 commit 329493a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -9,6 +9,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Added EXPERIMENTAL contains method to Mojo::Path.
- Added EXPERIMENTAL auto_upgrade attribute to Mojo::Content::Single.
- Added EXPERIMENTAL boundary method to Mojo::Content.
- Added EXPERIMENTAL slice method to Mojo::Collection.
- Improved many modules to use events instead of callbacks.
- Improved message parser performance slightly.
- Improved Mojo::IOLoop to die if started twice.
Expand Down
11 changes: 11 additions & 0 deletions lib/Mojo/Collection.pm
Expand Up @@ -66,6 +66,11 @@ sub shuffle {

sub size { scalar @{$_[0]} }

sub slice {
my $self = shift;
$self->new(@$self[@_]);
}

sub sort {
my ($self, $cb) = @_;
return $self->new(sort @$self) unless $cb;
Expand Down Expand Up @@ -163,6 +168,12 @@ from the results.
Create a new collection with all elements in reverse order.
=head2 C<slice>
my $new = $collection->slice(4 .. 7);
Create a new collection with all selected elements.
=head2 C<shuffle>
my $new = $collection->shuffle;
Expand Down
13 changes: 12 additions & 1 deletion t/mojo/collection.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 40;
use Test::More tests => 48;

# "'What are you lookin at?' - the innocent words of a drunken child."
use_ok 'Mojo::Collection', 'c';
Expand Down Expand Up @@ -94,3 +94,14 @@ $collection = c();
is_deeply [$collection->sort->each], [], 'no elements';
is_deeply [$collection->sort(sub { $_[1] cmp $_[0] })->each], [],
'no elements';

# Slice
$collection = c(1, 2, 3, 4, 5, 6, 7, 10, 9, 8);
is_deeply [$collection->slice(0)->each], [1], 'right result';
is_deeply [$collection->slice(1)->each], [2], 'right result';
is_deeply [$collection->slice(2)->each], [3], 'right result';
is_deeply [$collection->slice(-1)->each], [8], 'right result';
is_deeply [$collection->slice(-3, -5)->each], [10, 6], 'right result';
is_deeply [$collection->slice(1, 2, 3)->each], [2, 3, 4], 'right result';
is_deeply [$collection->slice(6, 1, 4)->each], [7, 2, 5], 'right result';
is_deeply [$collection->slice(6 .. 9)->each], [7, 10, 9, 8], 'right result';

0 comments on commit 329493a

Please sign in to comment.