Skip to content

Commit

Permalink
head and tail methods for Mojo::Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Grinnz committed Jul 31, 2019
1 parent de6f812 commit faf5056
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use ExtUtils::MakeMaker;

# JSON::PP 2.27103 first shipped with Perl 5.13.9
# Time::Local 1.2 first shipped with Perl 5.13.9
# List::Util 1.41 first shipped with Perl 5.21.4
# IO::Socket::IP 0.37 first shipped with Perl 5.21.11
# List::Util 1.50 first shipped with Perl 5.27.10
WriteMakefile(
NAME => 'Mojolicious',
VERSION_FROM => 'lib/Mojolicious.pm',
Expand All @@ -34,7 +34,7 @@ WriteMakefile(
},
PREREQ_PM => {
'IO::Socket::IP' => '0.37',
'List::Util' => '1.41',
'List::Util' => '1.50',
'JSON::PP' => '2.27103',
'Time::Local' => '1.2'
},
Expand Down
32 changes: 32 additions & 0 deletions lib/Mojo/Collection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ sub grep {
return $self->new(grep { $_->$cb(@_) } @$self);
}

sub head { $_[0]->new(List::Util::head $_[1], @{$_[0]}) }

sub join {
Mojo::ByteStream->new(join $_[1] // '', map {"$_"} @{$_[0]});
}
Expand Down Expand Up @@ -88,6 +90,8 @@ sub sort {
return $self->new(@sorted);
}

sub tail { $_[0]->new(List::Util::tail $_[1], @{$_[0]}) }

sub tap { shift->Mojo::Base::tap(@_) }

sub to_array { [@{shift()}] }
Expand Down Expand Up @@ -243,6 +247,20 @@ C<$_>.
# Find all values that are greater than 5
my $greater = $collection->grep(sub { $_ > 5 });
=head2 head
my $new = $collection->head(4);
my $new = $collection->head(-2);
Create a new collection with up to the specified number of elements from the
beginning of the collection. A negative number will count from the end.
# "A B C"
c('A', 'B', 'C', 'D', 'E')->head(3)->join(' ');
# "A B"
c('A', 'B', 'C', 'D', 'E')->head(-3)->join(' ');
=head2 join
my $stream = $collection->join;
Expand Down Expand Up @@ -337,6 +355,20 @@ time the callback is executed.
# Sort values case-insensitive
my $case_insensitive = $collection->sort(sub { uc($a) cmp uc($b) });
=head2 tail
my $new = $collection->tail(4);
my $new = $collection->tail(-2);
Create a new collection with up to the specified number of elements from the
end of the collection. A negative number will count from the beginning.
# "C D E"
c('A', 'B', 'C', 'D', 'E')->tail(3)->join(' ');
# "D E"
c('A', 'B', 'C', 'D', 'E')->tail(-3)->join(' ');
=head2 tap
$collection = $collection->tap(sub {...});
Expand Down
24 changes: 24 additions & 0 deletions t/mojo/collection.t
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,28 @@ is_deeply $collection->uniq(sub {$_})->to_array, [undef, 3, 2, 1, 0],
# TO_JSON
is encode_json(c(1, 2, 3)), '[1,2,3]', 'right result';

# head
$collection = c(1, 2, 5, 4, 3);
is_deeply $collection->head(0)->to_array, [], 'right result';
is_deeply $collection->head(1)->to_array, [1], 'right result';
is_deeply $collection->head(2)->to_array, [1, 2], 'right result';
is_deeply $collection->head(-1)->to_array, [1, 2, 5, 4], 'right result';
is_deeply $collection->head(-3)->to_array, [1, 2], 'right result';
is_deeply $collection->head(5)->to_array, [1, 2, 5, 4, 3], 'right result';
is_deeply $collection->head(6)->to_array, [1, 2, 5, 4, 3], 'right result';
is_deeply $collection->head(-5)->to_array, [], 'right result';
is_deeply $collection->head(-6)->to_array, [], 'right result';

# tail
$collection = c(1, 2, 5, 4, 3);
is_deeply $collection->tail(0)->to_array, [], 'right result';
is_deeply $collection->tail(1)->to_array, [3], 'right result';
is_deeply $collection->tail(2)->to_array, [4, 3], 'right result';
is_deeply $collection->tail(-1)->to_array, [2, 5, 4, 3], 'right result';
is_deeply $collection->tail(-3)->to_array, [4, 3], 'right result';
is_deeply $collection->tail(5)->to_array, [1, 2, 5, 4, 3], 'right result';
is_deeply $collection->tail(6)->to_array, [1, 2, 5, 4, 3], 'right result';
is_deeply $collection->tail(-5)->to_array, [], 'right result';
is_deeply $collection->tail(-6)->to_array, [], 'right result';

done_testing();

0 comments on commit faf5056

Please sign in to comment.