Skip to content

Commit

Permalink
remove the prototypes for map and filter
Browse files Browse the repository at this point in the history
  • Loading branch information
glennj committed Nov 15, 2024
1 parent d7443d3 commit 996b4cc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
12 changes: 4 additions & 8 deletions exercises/practice/list-ops/.meta/solutions/lib/ListOps.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ sub concat ($lists) {
foldl sub ( $acc, $list ) { append $acc, $list }, [], $lists;
}

sub map :prototype(&$) ( $func, $list ) {
my $f = sub ( $acc, $el ) {
local $_ = $el;
append $acc, [ $func->() ];
};
sub map ( $func, $list ) {
my $f = sub ( $acc, $el ) { append $acc, [ $func->($el) ] };
foldl $f, [], $list;
}

sub filter :prototype(&$) ( $func, $list ) {
sub filter ( $func, $list ) {
my $f = sub ( $acc, $el ) {
local $_ = $el;
$acc = append $acc, [$el] if $func->();
$acc = append $acc, [$el] if $func->($el);
$acc;
};
foldl $f, [], $list;
Expand Down
24 changes: 10 additions & 14 deletions exercises/practice/list-ops/.meta/template-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ properties:
if ($case->{input}{function} ne '(x) -> x modulo 2 == 1') {
die "template-data needs updating for new canonical data: $case->{description}";
}
sprintf(<<'END', '{$_ % 2 == 1}', map {dmp($_)} $case->{input}{list}, $case->{expected}, $case->{description});
sprintf(<<'END', 'sub ($el) {$el % 2 == 1}', map {dmp($_)} $case->{input}{list}, $case->{expected}, $case->{description});
{
my $filtered = ListOps::filter %s %s;
my $filtered = ListOps::filter %s, %s;
is(
$filtered,
%s,
Expand Down Expand Up @@ -68,9 +68,9 @@ properties:
if ($case->{input}{function} ne '(x) -> x + 1') {
die "template-data needs updating for new canonical data: $case->{description}";
}
sprintf(<<'END', '{$_ + 1}', map {dmp($_)} $case->{input}{list}, $case->{expected}, $case->{description});
sprintf(<<'END', 'sub ($el) {$el + 1}', map {dmp($_)} $case->{input}{list}, $case->{expected}, $case->{description});
{
my $mapped = ListOps::map %s %s;
my $mapped = ListOps::map %s, %s;
is(
$mapped,
%s,
Expand Down Expand Up @@ -158,15 +158,15 @@ stub: |-
return undef;
}
sub filter :prototype(&$) ($func, $list) {
sub filter ($func, $list) {
return undef;
}
sub length ($list) {
return undef;
}
sub map :prototype(&$) ($func, $list) {
sub map ($func, $list) {
return undef;
}
Expand Down Expand Up @@ -200,18 +200,14 @@ example: |-
foldl sub ($acc, $list) { append $acc, $list }, [], $lists;
}
sub map :prototype(&$) ($func, $list) {
my $f = sub ($acc, $el) {
local $_ = $el;
append $acc, [$func->()]
};
sub map ($func, $list) {
my $f = sub ($acc, $el) { append $acc, [$func->($el)] };
foldl $f, [], $list;
}
sub filter :prototype(&$) ($func, $list) {
sub filter ($func, $list) {
my $f = sub ($acc, $el) {
local $_ = $el;
$acc = append $acc, [$el] if $func->();
$acc = append $acc, [$el] if $func->($el);
$acc;
};
foldl $f, [], $list;
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/list-ops/lib/ListOps.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ sub concat ($lists) {
return undef;
}

sub filter :prototype(&$) ( $func, $list ) {
sub filter ( $func, $list ) {
return undef;
}

sub length ($list) {
return undef;
}

sub map :prototype(&$) ( $func, $list ) {
sub map ( $func, $list ) {
return undef;
}

Expand Down
8 changes: 4 additions & 4 deletions exercises/practice/list-ops/t/list-ops.t
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use experimental qw<signatures>;
} # end: d6ecd72c-197f-40c3-89a4-aa1f45827e09

{ # begin: 0524fba8-3e0f-4531-ad2b-f7a43da86a16
my $filtered = ListOps::filter { $_ % 2 == 1 } [];
my $filtered = ListOps::filter sub ($el) { $el % 2 == 1 }, [];
is(
$filtered,
[],
Expand All @@ -80,7 +80,7 @@ use experimental qw<signatures>;
} # end: 0524fba8-3e0f-4531-ad2b-f7a43da86a16

{ # begin: 88494bd5-f520-4edb-8631-88e415b62d24
my $filtered = ListOps::filter { $_ % 2 == 1 } [ 1, 2, 3, 5 ];
my $filtered = ListOps::filter sub ($el) { $el % 2 == 1 }, [ 1, 2, 3, 5 ];
is(
$filtered,
[ 1, 3, 5 ],
Expand All @@ -107,7 +107,7 @@ use experimental qw<signatures>;
} # end: d7b8d2d9-2d16-44c4-9a19-6e5f237cb71e

{ # begin: c0bc8962-30e2-4bec-9ae4-668b8ecd75aa
my $mapped = ListOps::map { $_ + 1 } [];
my $mapped = ListOps::map sub ($el) { $el + 1 }, [];
is(
$mapped,
[],
Expand All @@ -116,7 +116,7 @@ use experimental qw<signatures>;
} # end: c0bc8962-30e2-4bec-9ae4-668b8ecd75aa

{ # begin: 11e71a95-e78b-4909-b8e4-60cdcaec0e91
my $mapped = ListOps::map { $_ + 1 } [ 1, 3, 5, 7 ];
my $mapped = ListOps::map sub ($el) { $el + 1 }, [ 1, 3, 5, 7 ];
is(
$mapped,
[ 2, 4, 6, 8 ],
Expand Down

0 comments on commit 996b4cc

Please sign in to comment.