File: folds.t

package info (click to toggle)
liblist-objects-withutils-perl 2.028003-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,296 kB
  • sloc: perl: 1,957; makefile: 17; sh: 1
file content (30 lines) | stat: -rw-r--r-- 862 bytes parent folder | download | duplicates (4)
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
use Test::More;
use strict; use warnings FATAL => 'all';

use List::Objects::WithUtils 'array';

my $sum = sub { $_[0] + $_[1] };
my $arr = array(1 .. 3);

cmp_ok $arr->reduce($sum), '==', 6, 'reduce with positional args ok';
cmp_ok array(1)->reduce($sum), '==', 1, 'array with one element reduce ok';
ok !defined array->reduce($sum), 'empty array reduce ok';

cmp_ok array(6, 3, 2)->reduce(sub { $a / $b }), '==', 1,
  'reduce folds left (with named args)';

cmp_ok array(6, 3, 2)->foldl(sub { $a / $b }), '==', 1,
  'foldl folds left';

cmp_ok array(6, 3, 2)->fold_left(sub { $a / $b }), '==', 1,
  'fold_left alias ok';

cmp_ok array(2, 3, 6)->foldr(sub { $b / $a }), '==', 1,
  'foldr folds right';

cmp_ok array(2, 3, 6)->fold_right(sub { $b / $a }), '==', 1,
  'fold_right alias ok';

ok !defined array->foldr($sum), 'empty array foldr ok';

done_testing;