Skip to content

Commit

Permalink
Switch matrix from OO to functions (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-dango authored Apr 4, 2024
1 parent 47cada2 commit 362f40f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 43 deletions.
16 changes: 8 additions & 8 deletions exercises/practice/matrix/.meta/solutions/lib/Matrix.pm
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package Matrix;

use Moo;
use strict;
use warnings;
use experimental qw<signatures postderef postderef_qq>;

has string => (
is => 'ro',
);
use Exporter qw<import>;
our @EXPORT_OK = qw<extract_row extract_column>;

sub row ( $self, $index ) {
return [ split / /, [ split /\n/, $self->string ]->[ $index - 1 ] ];
sub extract_row ( $matrix, $row ) {
return [ split / /, [ split /\n/, $matrix ]->[ $row - 1 ] ];
}

sub column ( $self, $index ) {
return [ map { [ split / /, $_ ]->[ $index - 1 ] } split /\n/, $self->string ];
sub extract_column ( $matrix, $column ) {
return [ map { [ split / /, $_ ]->[ $column - 1 ] } split /\n/, $matrix ];
}

1;
31 changes: 11 additions & 20 deletions exercises/practice/matrix/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -1,52 +1,43 @@
subs: extract_row extract_column

properties:
row:
test: |-
use Data::Dmp;
$Data::Dmp::OPT_STRINGIFY_NUMBERS = 1;
local $Data::Dmp::OPT_STRINGIFY_NUMBERS = 1;
sprintf(<<'END', dmp($case->{input}{string}), $case->{input}{index}, map {dmp($_)} @{$case}{qw<expected description>});
is(
Matrix->new( string => %s )->row(%s),
extract_row(%s, %s),
%s,
%s,
);
END
column:
test: |-
use Data::Dmp;
$Data::Dmp::OPT_STRINGIFY_NUMBERS = 1;
local $Data::Dmp::OPT_STRINGIFY_NUMBERS = 1;
sprintf(<<'END', dmp($case->{input}{string}), $case->{input}{index}, map {dmp($_)} @{$case}{qw<expected description>});
is(
Matrix->new( string => %s )->column(%s),
extract_column(%s, %s),
%s,
%s,
);
END
moo: true

example: |-
has string => (
is => 'ro',
);
sub row ($self, $index) {
return [split / /, [split /\n/, $self->string]->[$index - 1]];
sub extract_row ($matrix, $row) {
return [split / /, [split /\n/, $matrix]->[$row - 1]];
}
sub column ($self, $index) {
return [map { [split / /, $_]->[$index - 1] } split /\n/, $self->string];
sub extract_column ($matrix, $column) {
return [map { [split / /, $_]->[$column - 1] } split /\n/, $matrix];
}
stub: |-
has string => (
is => 'ro',
);
sub row ($self, $index) {
sub extract_row ($matrix, $row) {
return undef;
}
sub column ($self, $index) {
sub extract_column ($matrix, $column) {
return undef;
}
10 changes: 4 additions & 6 deletions exercises/practice/matrix/lib/Matrix.pm
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package Matrix;

use v5.38;
use Moo;

has string => (
is => 'ro',
);
use Exporter qw<import>;
our @EXPORT_OK = qw<extract_row extract_column>;

sub row ( $self, $index ) {
sub extract_row ( $matrix, $row ) {
return undef;
}

sub column ( $self, $index ) {
sub extract_column ( $matrix, $column ) {
return undef;
}
18 changes: 9 additions & 9 deletions exercises/practice/matrix/t/matrix.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,52 @@ use Test2::V0;
use FindBin qw<$Bin>;
use lib "$Bin/../lib", "$Bin/../local/lib/perl5";

use Matrix ();
use Matrix qw<extract_row extract_column>;

is( # begin: ca733dab-9d85-4065-9ef6-a880a951dafd
Matrix->new( string => "1" )->row(1),
extract_row( "1", 1 ),
["1"],
"extract row from one number matrix",
); # end: ca733dab-9d85-4065-9ef6-a880a951dafd

is( # begin: 5c93ec93-80e1-4268-9fc2-63bc7d23385c
Matrix->new( string => "1 2\n3 4" )->row(2),
extract_row( "1 2\n3 4", 2 ),
[ "3", "4" ],
"can extract row",
); # end: 5c93ec93-80e1-4268-9fc2-63bc7d23385c

is( # begin: 2f1aad89-ad0f-4bd2-9919-99a8bff0305a
Matrix->new( string => "1 2\n10 20" )->row(2),
extract_row( "1 2\n10 20", 2 ),
[ "10", "20" ],
"extract row where numbers have different widths",
); # end: 2f1aad89-ad0f-4bd2-9919-99a8bff0305a

is( # begin: 68f7f6ba-57e2-4e87-82d0-ad09889b5204
Matrix->new( string => "1 2 3\n4 5 6\n7 8 9\n8 7 6" )->row(4),
extract_row( "1 2 3\n4 5 6\n7 8 9\n8 7 6", 4 ),
[ "8", "7", "6" ],
"can extract row from non-square matrix with no corresponding column",
); # end: 68f7f6ba-57e2-4e87-82d0-ad09889b5204

is( # begin: e8c74391-c93b-4aed-8bfe-f3c9beb89ebb
Matrix->new( string => "1" )->column(1),
extract_column( "1", 1 ),
["1"],
"extract column from one number matrix",
); # end: e8c74391-c93b-4aed-8bfe-f3c9beb89ebb

is( # begin: 7136bdbd-b3dc-48c4-a10c-8230976d3727
Matrix->new( string => "1 2 3\n4 5 6\n7 8 9" )->column(3),
extract_column( "1 2 3\n4 5 6\n7 8 9", 3 ),
[ "3", "6", "9" ],
"can extract column",
); # end: 7136bdbd-b3dc-48c4-a10c-8230976d3727

is( # begin: ad64f8d7-bba6-4182-8adf-0c14de3d0eca
Matrix->new( string => "1 2 3 4\n5 6 7 8\n9 8 7 6" )->column(4),
extract_column( "1 2 3 4\n5 6 7 8\n9 8 7 6", 4 ),
[ "4", "8", "6" ],
"can extract column from non-square matrix with no corresponding row",
); # end: ad64f8d7-bba6-4182-8adf-0c14de3d0eca

is( # begin: 9eddfa5c-8474-440e-ae0a-f018c2a0dd89
Matrix->new( string => "89 1903 3\n18 3 1\n9 4 800" )->column(2),
extract_column( "89 1903 3\n18 3 1\n9 4 800", 2 ),
[ "1903", "3", "4" ],
"extract column where numbers have different widths",
); # end: 9eddfa5c-8474-440e-ae0a-f018c2a0dd89
Expand Down

0 comments on commit 362f40f

Please sign in to comment.