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 39
|
#! /usr/bin/perl
use strict;
use warnings;
use Test::More;
use Perlude::Lazy;
my @tests =
( []
, [undef]
, [1..10]
, ['']
, ["haha"]
);
plan tests => 3*@tests;
for my $t (@tests) {
is_deeply
( [fold unfold @$t]
, $t
, "fold unfold => id"
)
}
# fold in void context
for my $t (@tests) {
my @l = @$t;
my $n = 1+@l;
#fold apply { $n--; @_ } unfold @$t;
fold enlist sub { $n--; @l ? (shift @l) : () };
is $n, 0, (0+@$t)." elements in void context";
}
# fold in scalar context
for my $t (@tests) {
my $n = fold unfold @$t;
is $n, 0+@$t, (0+@$t)." elements in scalar context";
}
|