1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use List::Keywords 'reductions';
# Short cornercases
{
is( [ reductions { die "ARGH" } () ], [],
'reductions on empty list yields empty' );
is( [ reductions { die "ARGH" } 123 ], [ 123 ],
'reductions on singleton list yields value directly' );
}
# basic sum
is(
[ reductions { $a + $b } 1 .. 5 ],
[ 1, 3, 6, 10, 15 ],
'partial sums of 1..5'
);
# reduce is definitely a left-fold
is(
[ reductions { "($a+$b)" } "a" .. "d" ],
[ "a", "(a+b)", "((a+b)+c)", "(((a+b)+c)+d)" ],
'reductions is a left-fold' );
# We don't guarantee what this will return but it definitely shouldn't crash
{
my $ret = reductions { $a + $b } 1 .. 5;
pass( 'reductions in scalar context does not crash' );
}
done_testing;
|