diff --git a/exercises/practice/matrix/.meta/solutions/lib/Matrix.pm b/exercises/practice/matrix/.meta/solutions/lib/Matrix.pm index d63c2dbe..916b796d 100644 --- a/exercises/practice/matrix/.meta/solutions/lib/Matrix.pm +++ b/exercises/practice/matrix/.meta/solutions/lib/Matrix.pm @@ -1,18 +1,18 @@ package Matrix; -use Moo; +use strict; +use warnings; use experimental qw; -has string => ( - is => 'ro', -); +use Exporter qw; +our @EXPORT_OK = qw; -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; diff --git a/exercises/practice/matrix/.meta/template-data.yaml b/exercises/practice/matrix/.meta/template-data.yaml index 172bea3b..e34239c3 100644 --- a/exercises/practice/matrix/.meta/template-data.yaml +++ b/exercises/practice/matrix/.meta/template-data.yaml @@ -1,12 +1,13 @@ +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}); is( - Matrix->new( string => %s )->row(%s), + extract_row(%s, %s), %s, %s, ); @@ -14,39 +15,29 @@ properties: 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}); 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; } diff --git a/exercises/practice/matrix/lib/Matrix.pm b/exercises/practice/matrix/lib/Matrix.pm index ff14cab3..df8a71b5 100644 --- a/exercises/practice/matrix/lib/Matrix.pm +++ b/exercises/practice/matrix/lib/Matrix.pm @@ -1,16 +1,14 @@ package Matrix; use v5.38; -use Moo; -has string => ( - is => 'ro', -); +use Exporter qw; +our @EXPORT_OK = qw; -sub row ( $self, $index ) { +sub extract_row ( $matrix, $row ) { return undef; } -sub column ( $self, $index ) { +sub extract_column ( $matrix, $column ) { return undef; } diff --git a/exercises/practice/matrix/t/matrix.t b/exercises/practice/matrix/t/matrix.t index 5af7c843..4bb3894b 100755 --- a/exercises/practice/matrix/t/matrix.t +++ b/exercises/practice/matrix/t/matrix.t @@ -4,52 +4,52 @@ use Test2::V0; use FindBin qw<$Bin>; use lib "$Bin/../lib", "$Bin/../local/lib/perl5"; -use Matrix (); +use Matrix qw; 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