File: 02mutually.t

package info (click to toggle)
libsub-recursive-perl 0.05-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: perl: 60; makefile: 2
file content (19 lines) | stat: -rw-r--r-- 542 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use Test::More tests => 1 + 3;
BEGIN { require_ok('Sub::Recursive') };

#########################

use strict;

BEGIN { use_ok('Sub::Recursive', qw/ mutually_recursive %REC /) }

my ($odd, $even) = mutually_recursive(
    odd  => sub { $_[0] == 0 ? 0 : $REC{even}->($_[0] - 1) },
    even => sub { $_[0] == 0 ? 1 : $REC{odd }->($_[0] - 1) },
);

my @odd  = map $odd ->($_), 1 .. 10;
my @even = map $even->($_), 1 .. 10;

is_deeply(\@odd , [ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 ], 'odd');
is_deeply(\@even, [ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ], 'even');