Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daxim/1391924 to your computer and use it in GitHub Desktop.
Save daxim/1391924 to your computer and use it in GitHub Desktop.
From 003e4a86a534576eee2cb26507cba038ef213048 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lars=20D=C9=AA=E1=B4=87=E1=B4=84=E1=B4=8B=E1=B4=8F=E1=B4=A1=20?=
=?UTF-8?q?=E8=BF=AA=E6=8B=89=E6=96=AF?= <daxim@cpan.org>
Date: Thu, 24 Nov 2011 18:30:38 +0100
Subject: [PATCH] Array->rotate <https://github.com/schwern/perl5i/issues/146>
The greater share of the work on this feature has been done by
student Vladimir during the Google Code-in 2011.
---
lib/perl5i.pm | 14 ++++++++++
lib/perl5i/2/ARRAY.pm | 19 +++++++++++++
t/rotate.t | 70 +++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 103 insertions(+), 0 deletions(-)
create mode 100644 t/rotate.t
diff --git a/lib/perl5i.pm b/lib/perl5i.pm
index df1c98d..1a62041 100644
--- a/lib/perl5i.pm
+++ b/lib/perl5i.pm
@@ -688,6 +688,20 @@ As with C<diff()>, it works with any number of arrays, nested data
structures of arbitrary depth, and handles overloaded objects
graciously.
+=head3 rotate
+
+Thinking of an array of strings as rows and columns, C<rotate> flips the
+columns and rows. So:
+
+ ["AAA", "BBB", "123"]->rotate
+ # ("AB1", "AB2", "AB3")
+
+ ["A", "BBB", "1234"]->rotate
+ # ("AB1", " B2", " B3", " 4")
+
+Short rows are filled in by default with spaces. When you pass an argument to
+C<rotate>, this is used instead of spaces.
+
=head3 ltrim
=head3 rtrim
diff --git a/lib/perl5i/2/ARRAY.pm b/lib/perl5i/2/ARRAY.pm
index 8950dc4..ce3a611 100644
--- a/lib/perl5i/2/ARRAY.pm
+++ b/lib/perl5i/2/ARRAY.pm
@@ -223,5 +223,24 @@ method trim($charset) {
return wantarray ? @result : \@result;
}
+method rotate($char){
+ return wantarray ? @$self : $self unless @$self;
+ my ($tmp, $max, @result) = ([],0);
+ foreach my $item (@$self){
+ my @row = split ('', $item);
+ push @row, '' if defined $item && !@row;
+ push @$tmp, \@row;
+ $max = scalar @row if $max < scalar @row;
+ }
+ foreach my $i (0..$max-1){
+ my $line = '';
+ foreach my $j (0..@$self-1){
+ $tmp->[$j]->[$i] ||= $char if defined $char;
+ $line .= $tmp->[$j]->[$i] if defined $tmp->[$j]->[$i];
+ }
+ push @result, $line
+ }
+ return wantarray ? @result : \@result;
+}
1;
diff --git a/t/rotate.t b/t/rotate.t
new file mode 100644
index 0000000..5c115ac
--- /dev/null
+++ b/t/rotate.t
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+use perl5i::latest;
+
+use lib 't/lib';
+use Test::More;
+use Test::perl5i;
+
+is_deeply(
+ scalar [ 'foo', 'bar', 'baz' ]->rotate,
+ [ 'fbb', 'oaa', 'orz' ],
+ 'Right array rotate'
+);
+
+is_deeply(
+ scalar [qw(AAA BBB 123)]->rotate,
+ [qw(AB1 AB2 AB3)]
+);
+
+is_deeply(
+ scalar [qw(A BBB 1234)]->rotate,
+ ["AB1", "B2", "B3", "4"]
+);
+
+is_deeply(
+ scalar [qw(AAA23 AB1D1 A1BC AAB212)]->rotate,
+ ["AAAA", "AB1A", "A1BB", "2DC2", "311", "2",]
+);
+
+is_deeply(
+ scalar ['AB C']->rotate->rotate,
+ ['AB C'],
+ 'rotated twice is identical'
+);
+
+is_deeply(
+ scalar []->rotate,
+ [],
+);
+
+is_deeply(
+ scalar ['']->rotate,
+ [''],
+);
+
+is_deeply(
+ scalar ['A']->rotate,
+ ['A'],
+);
+
+is_deeply(
+ scalar ['00']->rotate,
+ ['0', '0'],
+);
+
+is_deeply(
+ scalar ['','']->rotate,
+ [''],
+);
+
+is_deeply(
+ scalar [qw(A BBB 1234)]->rotate('x'),
+ ["AB1", "xB2", "xB3", "xx4"]
+);
+
+is_deeply(
+ scalar [qw(AAA23 AB1D1 A1BC AAB212)]->rotate('x'),
+ ["AAAA", "AB1A", "A1BB", "2DC2", "31x1", "xxx2",]
+);
+
+done_testing;
--
1.7.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment