-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch matrix from OO to functions (#656)
- Loading branch information
Showing
4 changed files
with
32 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters