Add function filter and a test
This commit is contained in:
parent
1df85fd67c
commit
4a42695a27
2 changed files with 11 additions and 0 deletions
|
@ -284,6 +284,13 @@ my %stdctx = (
|
|||
$v = $cb->($v,$_) foreach (@copy);
|
||||
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 {
|
||||
my ($v, $list) = @_;
|
||||
return [ $v, @$list ];
|
||||
|
|
|
@ -34,3 +34,7 @@
|
|||
|
||||
(expect "short notation can create empty lists"
|
||||
(zerop (length '())))
|
||||
|
||||
(expect "filter - evenp"
|
||||
(equal (filter evenp '(1 2 3 4 5 6))
|
||||
'(2 4 6)))
|
||||
|
|
Loading…
Reference in a new issue