File Coverage

blib/lib/Moose/Autobox/Code.pm
Criterion Covered Total %
statement 33 33 100.0
branch 6 6 100.0
condition n/a
subroutine 17 17 100.0
pod 7 7 100.0
total 63 63 100.0


line stmt bran cond sub pod time code
1             package Moose::Autobox::Code;
2             # ABSTRACT: the Code role
3 14     14   64 use Moose::Role 'with';
  14         22  
  14         101  
4 14     14   40741 use Moose::Autobox;
  14         28  
  14         89  
5 14     14   5800 use namespace::autoclean;
  14         20  
  14         105  
6              
7             our $VERSION = '0.16';
8              
9             with 'Moose::Autobox::Ref';
10              
11             sub curry {
12 1     1 1 3 my ($f, @a) = @_;
13 1     1   3 return sub { $f->(@a, @_) }
14 1         7 }
15              
16             sub rcurry {
17 1     1 1 4 my ($f, @a) = @_;
18 1     1   467 return sub { $f->(@_, @a) }
19 1         4 }
20              
21             sub compose {
22 2     2 1 4 my ($f, $f2, @rest) = @_;
23 2 100       9 return $f if !$f2;
24 1     1   33 return (sub { $f2->($f->(@_)) })->compose(@rest);
  1         6  
25             }
26              
27             sub disjoin {
28 2     2 1 415 my ($f, $f2) = @_;
29 2 100   2   4 return sub { $f->(@_) || $f2->(@_) }
30 2         8 }
31              
32             sub conjoin {
33 2     2 1 545 my ($f, $f2) = @_;
34 2 100   2   5 return sub { $f->(@_) && $f2->(@_) }
35 2         8 }
36              
37             # fixed point combinators
38              
39             sub u {
40 12     12 1 11 my $f = shift;
41 12     12   24 sub { $f->($f, @_) };
  12         889  
42             }
43              
44             sub y {
45 1     1 1 97 my $f = shift;
46 1     11   8 (sub { my $h = shift; sub { $f->(($h->u)->())->(@_) } }->u)->();
  11         7  
  11         25  
  10         66  
47             }
48              
49             1;
50              
51             __END__
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             Moose::Autobox::Code - the Code role
60              
61             =head1 VERSION
62              
63             version 0.16
64              
65             =head1 SYNOPSIS
66              
67             use Moose::Autobox;
68              
69             my $adder = sub { $_[0] + $_[1] };
70             my $add_2 = $adder->curry(2);
71              
72             $add_2->(2); # returns 4
73              
74             # create a recursive subroutine
75             # using the Y combinator
76             *factorial = sub {
77             my $f = shift;
78             sub {
79             my $n = shift;
80             return 1 if $n < 2;
81             return $n * $f->($n - 1);
82             }
83             }->y;
84              
85             factorial(10) # returns 3628800
86              
87             =head1 DESCRIPTION
88              
89             This is a role to describe operations on the Code type.
90              
91             =head1 METHODS
92              
93             =over 4
94              
95             =item C<curry (@values)>
96              
97             =item C<rcurry (@values)>
98              
99             =item C<conjoin (\&sub)>
100              
101             =item C<disjoin (\&sub)>
102              
103             =item C<compose (@subs)>
104              
105             This will take a list of C<@subs> and compose them all into a single
106             subroutine where the output of one sub will be the input of another.
107              
108             =item C<y>
109              
110             =for :stopwords combinator
111              
112             This implements the Y combinator.
113              
114             =item C<u>
115              
116             This implements the U combinator.
117              
118             =back
119              
120             =over 4
121              
122             =item C<meta>
123              
124             =back
125              
126             =head1 SEE ALSO
127              
128             =over 4
129              
130             =item L<http://en.wikipedia.org/wiki/Fixed_point_combinator>
131              
132             =item L<http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/20469>
133              
134             =back
135              
136             =head1 SUPPORT
137              
138             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Moose-Autobox>
139             (or L<bug-Moose-Autobox@rt.cpan.org|mailto:bug-Moose-Autobox@rt.cpan.org>).
140              
141             There is also a mailing list available for users of this distribution, at
142             L<http://lists.perl.org/list/moose.html>.
143              
144             There is also an irc channel available for users of this distribution, at
145             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
146              
147             =head1 AUTHOR
148              
149             Stevan Little <stevan.little@iinteractive.com>
150              
151             =head1 COPYRIGHT AND LICENSE
152              
153             This software is copyright (c) 2006 by Infinity Interactive, Inc.
154              
155             This is free software; you can redistribute it and/or modify it under
156             the same terms as the Perl 5 programming language system itself.
157              
158             =cut