docs mostly
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Code.pm
CommitLineData
5f654d8e 1package Moose::Autobox::Code;
2use Moose::Role 'with';
7dad2765 3use Moose::Autobox;
5f654d8e 4
7dad2765 5our $VERSION = '0.02';
5f654d8e 6
7with 'Moose::Autobox::Ref';
8
9sub curry {
10 my ($f, @a) = @_;
11 return sub { $f->(@a, @_) }
12}
13
14sub rcurry {
15 my ($f, @a) = @_;
16 return sub { $f->(@_, @a) }
17}
18
19sub compose {
20 my ($f, $f2, @rest) = @_;
21 return $f if !$f2;
22 return (sub { $f2->($f->(@_)) })->compose(@rest);
23}
24
25sub disjoin {
26 my ($f, $f2) = @_;
27 return sub { $f->(@_) || $f2->(@_) }
28}
29
30sub conjoin {
31 my ($f, $f2) = @_;
32 return sub { $f->(@_) && $f2->(@_) }
33}
34
bb5a920e 35sub u {
36 my $f = shift;
37 sub { $f->($f, @_) };
38}
39
40sub y {
41 my $f = shift;
42 (sub { my $h = shift; sub { $f->(($h->u())->())->(@_) } }->u())->();
43}
44
7fc99864 45#sub dump {
46 #my ($self) = @_;
47 #require Data::Dump::Streamer;
48 #return Data::Dump::Streamer::Dump($self)->Out();
49#}
50
31d40d73 511;
52
53__END__
54
55=pod
56
57=head1 NAME
58
59Moose::Autobox::Code - the Code role
60
61=head1 SYNOPOSIS
62
63 use Moose::Autobox;
31d40d73 64
65 my $adder = sub { $_[0] + $_[1] };
5272f13f 66 my $add_2 = $adder->curry(2);
31d40d73 67
68 $add_2->(2); # returns 4
bb5a920e 69
70 # create a recursive subroutine
71 # using the Y combinator
72 *factorial = sub {
73 my $f = shift;
74 sub {
75 my $n = shift;
76 return 1 if $n < 2;
77 return $n * $f->($n - 1);
78 }
79 }->y;
80
81 factorial(10) # returns 3628800
82
31d40d73 83
84=head1 DESCRIPTION
85
8937074a 86This is a role to describe operations on the Code type.
87
260cc81f 88=head1 METHODS
89
90=over 4
91
5272f13f 92=item B<curry (@values)>
93
94=item B<rcurry (@values)>
95
96=item B<conjoin (\&sub)>
260cc81f 97
5272f13f 98=item B<disjoin (\&sub)>
260cc81f 99
5272f13f 100=item B<compose (@subs)>
260cc81f 101
5272f13f 102This will take a list of C<@subs> and compose them all into a single
103subroutine where the output of one sub will be the input of another.
260cc81f 104
bb5a920e 105=item B<y>
106
107This implements the Y combinator.
108
109=item B<u>
110
111This implements the U combinator.
112
5272f13f 113=back
114
115=over 4
260cc81f 116
5272f13f 117=item B<meta>
260cc81f 118
119=back
120
bb5a920e 121=head1 SEE ALSO
122
123=over 4
124
125=item L<http://en.wikipedia.org/wiki/Fixed_point_combinator>
126
127=back
128
31d40d73 129=head1 BUGS
130
131All complex software has bugs lurking in it, and this module is no
132exception. If you find a bug please either email me, or add the bug
133to cpan-RT.
134
135=head1 AUTHOR
136
137Stevan Little E<lt>stevan@iinteractive.comE<gt>
138
139=head1 COPYRIGHT AND LICENSE
140
141Copyright 2006 by Infinity Interactive, Inc.
142
143L<http://www.iinteractive.com>
144
145This library is free software; you can redistribute it and/or modify
146it under the same terms as Perl itself.
147
f6e003cc 148=cut