Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cycle operation is now destructive

  • Loading branch information...
commit 3e8b1ac4a1e0fe49f5633a39b640b7160aa36517 1 parent 7802838
@kentaro authored
Showing with 40 additions and 33 deletions.
  1. +3 −0  Changes
  2. +11 −11 README.mkdn
  3. +14 −13 lib/List/Rubyish/Circular.pm
  4. +12 −9 t/cycle.t
View
3  Changes
@@ -1,5 +1,8 @@
Revision history for Perl extension List::Rubyish::Circular
+0.04 Sun Apr 10 11:53:49 2011
+ - cycle/rcycle operations are now destructive
+
0.03 Sun Apr 10 00:57:30 2011
- added a prerequisite module
View
22 README.mkdn
@@ -10,18 +10,18 @@ List::Rubyish
my $list = List::Rubyish::Circular->new(qw(jkondo reikon cinnamon));
- is_deeply [qw(reikon cinnamon jkondo)], $list->cycle->to_a;
- is_deeply [qw(cinnamon jkondo reikon)], $list->cycle(2)->to_a;
+ is_deeply, $list->cycle->to_a, [qw(reikon cinnamon jkondo)];
+ is_deeply, $list->cycle(2)->to_a, [qw(jkondo reikon cinnamon)];
- is_deeply [qw(cinnamon jkondo reikon)], $list->rcycle->to_a;
- is_deeply [qw(reikon cinnamon jkondo)], $list->rcycle(2)->to_a;
+ is_deeply, $list->rcycle->to_a, [qw(cinnamon jkondo reikon)];
+ is_deeply, $list->rcycle(2)->to_a, [qw(jkondo reikon cinnamon)];
# $list is still a circular list after destracive operation
$list->push(qw(tokky));
- is_deeply [qw(jkondo reikon cinnamon tokky)], $list->to_a;
- is_deeply [qw(reikon cinnamon tokky jkondo)], $list->cycle->to_a;
- is_deeply [qw(cinnamon tokky jkondo reikon)], $list->rcycle(2)->to_a;
+ is_deeply, $list->to_a, [qw(jkondo reikon cinnamon tokky)];
+ is_deeply, $list->cycle->to_a, [qw(reikon cinnamon tokky jkondo)];
+ is_deeply, $list->rcycle(2)->to_a, [qw(tokky jkondo reikon cinnamon)];
# DESCRIPTION
@@ -33,13 +33,13 @@ List::Rubyish against a circular list.
## cycle ( I<$count> )
-Shifts list to the left according to `$count` and returns new list
-`$count`. If $count not passed in, its value is 1.
+Shifts list to the left according to `$count`. If $count not passed
+in, its value is 1. This operation is destructive.
my $list = List::Rubyish::Circular->new(qw(jkondo reikon cinnamon));
- is_deeply [qw(reikon cinnamon jkondo)], $list->cycle->to_a;
- is_deeply [qw(cinnamon jkondo reikon)], $list->cycle(2)->to_a;
+ is_deeply, $list->cycle->to_a, [qw(reikon cinnamon jkondo)];
+ is_deeply, $list->cycle(2)->to_a, [qw(jkondo reikon cinnamon)];
## rcycle ( I<$count> )
View
27 lib/List/Rubyish/Circular.pm
@@ -4,7 +4,7 @@ use strict;
use warnings;
use parent qw(List::Rubyish);
-our $VERSION = '0.03';
+our $VERSION = '0.04';
sub cycle {
my ($self, $count) = @_;
@@ -15,7 +15,8 @@ sub cycle {
push @list, $self->[$index % $self->size];
}
- $self->new(@list);
+ @{$self} = @list;
+ $self;
}
sub rcycle {
@@ -42,18 +43,18 @@ List::Rubyish
my $list = List::Rubyish::Circular->new(qw(jkondo reikon cinnamon));
- is_deeply [qw(reikon cinnamon jkondo)], $list->cycle->to_a;
- is_deeply [qw(cinnamon jkondo reikon)], $list->cycle(2)->to_a;
+ is_deeply, $list->cycle->to_a, [qw(reikon cinnamon jkondo)];
+ is_deeply, $list->cycle(2)->to_a, [qw(jkondo reikon cinnamon)];
- is_deeply [qw(cinnamon jkondo reikon)], $list->rcycle->to_a;
- is_deeply [qw(reikon cinnamon jkondo)], $list->rcycle(2)->to_a;
+ is_deeply, $list->rcycle->to_a, [qw(cinnamon jkondo reikon)];
+ is_deeply, $list->rcycle(2)->to_a, [qw(jkondo reikon cinnamon)];
# $list is still a circular list after destracive operation
$list->push(qw(tokky));
- is_deeply [qw(jkondo reikon cinnamon tokky)], $list->to_a;
- is_deeply [qw(reikon cinnamon tokky jkondo)], $list->cycle->to_a;
- is_deeply [qw(cinnamon tokky jkondo reikon)], $list->rcycle(2)->to_a;
+ is_deeply, $list->to_a, [qw(jkondo reikon cinnamon tokky)];
+ is_deeply, $list->cycle->to_a, [qw(reikon cinnamon tokky jkondo)];
+ is_deeply, $list->rcycle(2)->to_a, [qw(tokky jkondo reikon cinnamon)];
=head1 DESCRIPTION
@@ -67,13 +68,13 @@ List::Rubyish against a circular list.
=over 4
-Shifts list to the left according to C<$count> and returns new list
-C<$count>. If $count not passed in, its value is 1.
+Shifts list to the left according to C<$count>. If $count not passed
+in, its value is 1. This operation is destructive.
my $list = List::Rubyish::Circular->new(qw(jkondo reikon cinnamon));
- is_deeply [qw(reikon cinnamon jkondo)], $list->cycle->to_a;
- is_deeply [qw(cinnamon jkondo reikon)], $list->cycle(2)->to_a;
+ is_deeply, $list->cycle->to_a, [qw(reikon cinnamon jkondo)];
+ is_deeply, $list->cycle(2)->to_a, [qw(jkondo reikon cinnamon)];
=back
View
21 t/cycle.t
@@ -7,28 +7,31 @@ use List::Rubyish::Circular;
subtest 'cycle' => sub {
my $list = List::Rubyish::Circular->new(1, 2, 3, 4, 5);
- is_deeply [2, 3, 4, 5, 1], $list->cycle->to_a;
- is_deeply [3, 4, 5, 1, 2], $list->cycle(2)->to_a;
+
+ is_deeply $list->cycle->to_a, [2, 3, 4, 5, 1];
+ is_deeply $list->cycle(2)->to_a, [4, 5, 1, 2, 3];
};
subtest 'rcycle' => sub {
my $list = List::Rubyish::Circular->new(1, 2, 3, 4, 5);
- is_deeply [5, 1, 2, 3, 4], $list->rcycle->to_a;
- is_deeply [4, 5, 1, 2, 3], $list->rcycle(2)->to_a;
+ is_deeply $list->rcycle->to_a, [5, 1, 2, 3, 4];
+ is_deeply $list->rcycle(2)->to_a, [3, 4, 5, 1, 2];
};
subtest 'destractive opperation' => sub {
my $list = List::Rubyish::Circular->new(1, 2, 3);
- $list->push(4, 5);
- is_deeply [2, 3, 4, 5, 1], $list->cycle->to_a;
- is_deeply [3, 4, 5, 1, 2], $list->cycle(2)->to_a;
+ $list->push(4, 5);
+ is_deeply $list->to_a, [1, 2, 3, 4, 5];
+ is_deeply $list->cycle->to_a, [2, 3, 4, 5, 1];
+ is_deeply $list->cycle(2)->to_a, [4, 5, 1, 2, 3];
$list->unshift(-1, 0);
+ is_deeply $list->to_a, [-1, 0, 4, 5, 1, 2, 3];
- is_deeply [ 0, 1, 2, 3, 4, 5, -1 ], $list->cycle->to_a;
- is_deeply [ 1, 2, 3, 4, 5, -1, 0 ], $list->cycle(2)->to_a;
+ is_deeply $list->cycle->to_a, [0, 4, 5, 1, 2, 3, -1];
+ is_deeply $list->cycle(2)->to_a, [5, 1, 2, 3, -1, 0, 4];
};
done_testing;

0 comments on commit 3e8b1ac

Please sign in to comment.
Something went wrong with that request. Please try again.