Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

head and tail methods for Mojo::Collection #1360

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
head and tail methods for Mojo::Collection
Algorithms adapted from List::Util::PP
  • Loading branch information
Grinnz committed Jun 5, 2019
commit 256c88404042c6207a1c33dcd242938457190aae
42 changes: 42 additions & 0 deletions lib/Mojo/Collection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ sub grep {
return $self->new(grep { $_->$cb(@_) } @$self);
}

sub head {
my ($self, $size) = @_;
return $self->new(@$self) if $size > @$self;
return $self->new(@$self[0 .. ($size - 1)]) if $size >= 0;
return $self->new(@$self[0 .. ($#$self + $size)]);
}

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

sub tail {
my ($self, $size) = @_;
return $self->new(@$self) if $size > @$self;
return $self->new(@$self[($#$self - ($size - 1)) .. $#$self]) if $size >= 0;
return $self->new(@$self[(0 - $size) .. $#$self]);
}

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

sub to_array { [@{shift()}] }
Expand Down Expand Up @@ -243,6 +257,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 +365,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();