Add function filter and a test

This commit is contained in:
madmaurice 2021-04-04 02:34:05 +02:00
parent 1df85fd67c
commit 4a42695a27
2 changed files with 11 additions and 0 deletions

View file

@ -284,6 +284,13 @@ my %stdctx = (
$v = $cb->($v,$_) foreach (@copy); $v = $cb->($v,$_) foreach (@copy);
return $v; return $v;
}, },
'filter' => sub {
my ($cb, $list) = @_;
die "map: First parameter must be a function" unless ref($cb) eq "CODE";
die "map: Second parameter must be a list" unless ref($list) eq "ARRAY";
return [ grep { $cb->($_) } @$list ];
},
'cons' => sub { 'cons' => sub {
my ($v, $list) = @_; my ($v, $list) = @_;
return [ $v, @$list ]; return [ $v, @$list ];

View file

@ -34,3 +34,7 @@
(expect "short notation can create empty lists" (expect "short notation can create empty lists"
(zerop (length '()))) (zerop (length '())))
(expect "filter - evenp"
(equal (filter evenp '(1 2 3 4 5 6))
'(2 4 6)))