Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[NEEDS INPUT] building out the array transpose method as requested in #146 #242

Open
wants to merge 7 commits into from

2 participants

ben hengst Michael G. Schwern
ben hengst

While I am not exactly sure why one would use this and if it really belongs in core, it was an fun bit of coding. Currently it always outputs a array of strings even if it was handed a 2d array. I am not sure if this is really what we want to happen here. Also if does not check to see if $fill character is a single character or not, should it? It seems that if you are altering the default then you know what you are doing, though again as I can not really conceive why this is useful I am failing to find a case where this would help.

schwern and others added some commits
ben hengst

In talking with @schwern this should only take arrays of arrays OR arrays of strings. MIXED TYPES OR ANY OTHER REF => EXCEPTION!

[[1,2,3],[1,2,3]]->transpose; # [[1,1],[2,2],[3,3]]
['AA','BB','CC',]->transpose; # ['ABC','ABC']

Oh and make the option for transpose not positional rather make it take a hashref.

Michael G. Schwern
Owner

And one more example...

[
    [["F", "O", "O"], "BAR", "BAZ"],
    [1, 2, 3]
    ["A", "B", "C"]
]

transposes to...

[
    [["F", "O", "O"], 1, "A"],
    ["BAR", 2, "B"],
    ["BAZ", 3, "C"]
]

Which is to say, in the 2D array case all the items are treated as atoms.

Michael G. Schwern
Owner

Ping?

ben hengst added some commits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 20, 2009
  1. Michael G. Schwern

    Add IO::All but chop out the guessing about what it is. Only work with

    schwern authored
    files and leave everything else to be explicit.
    
    Also add io->url() for opening URLs.
  2. Michael G. Schwern

    Merge branch 'master' into io-all

    schwern authored
    Conflicts:
    	lib/perl5i.pm
Commits on Mar 21, 2013
Commits on Jun 14, 2013
  1. Merge branch 'io-all' of github.com:notbenh/perl5i

    ben hengst authored
    Conflicts:
    	Build.PL
    	lib/perl5i.pm
Commits on Jul 5, 2013
  1. Merge remote-tracking branch 'schwern/master'

    ben hengst authored
    Conflicts:
    	Build.PL
    	lib/perl5i.pm
  2. Merge branch 'master' into issue/146

    ben hengst authored
Commits on Jul 12, 2013
  1. adding the tests but this is not yet done

    ben hengst authored
This page is out of date. Refresh to see the latest.
18 lib/perl5i.pm
View
@@ -769,6 +769,24 @@ which will determine what characters should be trimmed.
my @trimmed = ['-> foo <-', '-> bar <-']->trim('-><'); # [' foo ', ' bar ']
+=head3 transpose
+
+ my @transposed = @list->transpose;
+ my @transposed = @list->transpose($fill_character);
+
+Treat @list as though it is comprised as rows and columns, flip the
+columns and rows:
+
+ my @transposed = ['AAA','BBB','123']->transpose; # ['AB1','AB2','AB3']
+
+If the length of string are unmatched then each 'blank' is filed with
+$fill_character who's default value is a single space.
+
+ my @transposed = ['AAA','BB' ,'C','1234']->transpose; # ['ABC1','AB 2','A 3',' 4']
+ my @transposed = ['AAA','BB' ,'C','1234']->transpose('_'); # ['ABC1','AB_2','A__3','___4']
+
+
+
=head2 Hash Autoboxing
9 lib/perl5i/2/ARRAY.pm
View
@@ -294,4 +294,13 @@ method trim($charset) {
}
+method transpose($fill){
+ $fill //= ' ';
+ my @parts = $self->map(sub{$_->mo->reftype?$_:[split //]});
+ my @results = map{my $i=$_; [map{$_->[$i] || $fill} @parts]->join('')} 0..$#parts;
+ use Data::Dumper;
+ warn Dumper(\@parts,\@results);
+ return wantarray ? @results : \@results;
+}
+
1;
82 lib/perl5i/IO.pm
View
@@ -0,0 +1,82 @@
+package perl5i::IO;
+
+use strict;
+use warnings;
+
+use IO::All;
+use IO::All::LWP;
+
+
+# This is IO::All->new but with most of the magic
+# stripped out.
+sub safer_io {
+ my $name = shift;
+
+ # No name, return an empty io() object.
+ return io() unless defined $name;
+
+ # Its an IO::All object, return it.
+ return $name if UNIVERSAL::isa($name, 'IO::All');
+
+ my $io = io();
+
+ # Its a filehandle
+ return $io->handle($name)
+ if UNIVERSAL::isa($name, 'GLOB') or ref(\ $name) eq 'GLOB';
+
+ # link is first because a link to a dir returns true for
+ # both -l and -d.
+ return $io->link($name) if -l $name;
+ return $io->file($name) if -f $name;
+ return $io->dir($name) if -d $name;
+
+ # Maybe its a file they're going to write to later
+ $io->name($name);
+ return $io;
+}
+
+
+{
+ package IO::All;
+
+ use strict;
+ use warnings;
+
+ sub url {
+ my $self = shift;
+ my $url = shift;
+
+ my($method) = $url =~ /^([a-z]+):/;
+ $method ||= "http";
+ $method = "file_url" if $method eq 'file';
+
+ # $self->can($scheme) or
+ # croak "url() does not know how to open scheme type $scheme";
+
+ return $self->$method($url);
+ }
+}
+
+
+{
+ package IO::All::FILE_URL;
+
+ # Convince IO::All that we exist
+ $INC{"IO/All/FILE_URL.pm"} = 1;
+
+ use strict;
+ use warnings;
+
+ use IO::All::LWP "-base";
+
+ const type => "file";
+
+ sub file_url {
+ my $self = shift;
+
+ return $self->lwp_init(__PACKAGE__, @_);
+ }
+}
+
+
+1;
32 t/array_rotate.t
View
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+use perl5i::latest;
+use Test::Most;
+
+is_deeply scalar([qw{AAA BBB 123}]->transpose) # uses wantarray
+ , [qw{AB1 AB2 AB3}]
+ , q{array transpose works with strings};
+
+is_deeply scalar([[qw{A A A}],[qw{B B B}], [qw{1 2 3}]]->transpose)
+ , [qw{AB1 AB2 AB3}]
+ , q{array transpose works with arrays};
+
+is_deeply scalar([qw{AAA BB C 1234}]->transpose)
+ , ['ABC1', 'AB 2', 'A 3', ' 4']
+ , q{array transpose works with unmaptched strings};
+
+is_deeply scalar([qw{AAA BB C 1234}]->transpose('_'))
+ , ['ABC1', 'AB_2', 'A__3', '___4']
+ , q{array transpose works with unmaptched strings};
+
+is_deeply scalar([ [[qw{F O O}], "BAR", "BAZ"]
+ , [1, 2, 3]
+ , ["A", "B", "C"]
+ ]),
+ , [ [["F", "O", "O"], 1, "A"]
+ , ["BAR", 2, "B"]
+ , ["BAZ", 3, "C"]
+ ]
+ , q{ are references handled? https://github.com/schwern/perl5i/pull/242#issuecomment-15318379 };
+
+
+done_testing;
29 t/io-url.t
View
@@ -0,0 +1,29 @@
+#!/usr/bin/perl -w
+
+use perl5i;
+use Test::More;
+use Test::Exception;
+
+chdir 't';
+
+# Test we can talk to a file URL
+{
+ "hello\nstuff\n" > io("io-url-test");
+ ok -e "io-url-test";
+ END { unlink "io-url-test" }
+
+ is io->url("file://$CWD/io-url-test")->slurp, "hello\nstuff\n";
+}
+
+
+# Test that we normally won't try to open a URL
+{
+ throws_ok {
+ io("http://www.google.com")->all;
+ } qr{^Can't open file 'http://www.google.com' for input},
+ "io() won't open a URL as a URL";
+}
+
+
+done_testing();
+
13 t/io.t
View
@@ -0,0 +1,13 @@
+#!/usr/bin/perl -w
+
+use perl5i;
+use Test::More;
+
+chdir 't';
+
+"hello" > io("io-test");
+END { unlink "io-test" }
+
+is io("io-test")->slurp, "hello";
+
+done_testing();
Something went wrong with that request. Please try again.