File: 006_y_combinator.t

package info (click to toggle)
libmoose-autobox-perl 0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 308 kB
  • sloc: perl: 2,454; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 683 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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 3;
use Test::Exception;

BEGIN {
    use_ok('Moose::Autobox');
}

use Moose::Autobox;

# we need a control in the experiment
sub fact {
    my $n = shift;
    return 1 if $n < 2;
    return $n * fact($n - 1);
}

*fact2 = sub {
    my $f = shift;
    sub {
        my $n = shift;
        return 1 if $n < 2;
        return $n * $f->($n - 1);
    }
}->y;

*fact3 = sub {
    my $f = shift;
    sub {
        my $n = shift;
        return 1 if $n < 2;
        return $n * ($f->($f))->($n - 1);
    }
}->u;

is(fact(10), fact2(10), '... our factorials match');
is(fact(10), fact3()->(10), '... our factorials match');