Add map, reduce and equal for lists

This commit is contained in:
madmaurice 2021-04-02 20:18:42 +02:00
parent 78c4bebaae
commit 7f38e74e78
2 changed files with 26 additions and 0 deletions

View file

@ -229,6 +229,28 @@ my %stdctx = (
'first' => sub { return (shift)->[0]; },
'second' => sub { return (shift)->[1]; },
'nth' => sub { my ($idx,$list) = @_; return $list->[$idx]; },
'map' => 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 [ map { $cb->($_) } @$list ];
},
'reduce' => 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";
my @copy = ( @$list );
my $v = shift @copy;
$v = $cb->($v,$_) foreach (@copy);
return $v;
},
# TODO: this is tricky as it needs to recurse into sub lists
'equal' => sub {
my ($a, $b) = @_;
return @$a == @$b;
},
# Constants
't' => 1,

View file

@ -4,4 +4,8 @@
(expect "First element is 1" (= (first lst) 1))
(expect "Second element is 2" (= (second lst) 2))
(expect "Third element is 3" (= (nth 2 lst) 3))
(expect "Equal to itself" (equal lst lst))
(expect "Equal to identical list" (equal lst (list 1 2 3)))
(expect "Reduce with +" (= (reduce + lst) 6))
(expect "Map doubling values" (equal (map (lambda (x) (* x 2)) lst) (list 2 4 6)))
)