1 package Moose::Autobox::Code;
2 use Moose::Role 'with';
7 with 'Moose::Autobox::Ref';
11 return sub { $f->(@a, @_) }
16 return sub { $f->(@_, @a) }
20 my ($f, $f2, @rest) = @_;
22 return (sub { $f2->($f->(@_)) })->compose(@rest);
27 return sub { $f->(@_) || $f2->(@_) }
32 return sub { $f->(@_) && $f2->(@_) }
35 # fixed point combinators
44 (sub { my $h = shift; sub { $f->(($h->u)->())->(@_) } }->u)->();
55 Moose::Autobox::Code - the Code role
61 my $adder = sub { $_[0] + $_[1] };
62 my $add_2 = $adder->curry(2);
64 $add_2->(2); # returns 4
66 # create a recursive subroutine
67 # using the Y combinator
73 return $n * $f->($n - 1);
77 factorial(10) # returns 3628800
82 This is a role to describe operations on the Code type.
88 =item B<curry (@values)>
90 =item B<rcurry (@values)>
92 =item B<conjoin (\&sub)>
94 =item B<disjoin (\&sub)>
96 =item B<compose (@subs)>
98 This will take a list of C<@subs> and compose them all into a single
99 subroutine where the output of one sub will be the input of another.
103 This implements the Y combinator.
107 This implements the U combinator.
121 =item L<http://en.wikipedia.org/wiki/Fixed_point_combinator>
123 =item L<http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/20469>
129 All complex software has bugs lurking in it, and this module is no
130 exception. If you find a bug please either email me, or add the bug
135 Stevan Little E<lt>stevan@iinteractive.comE<gt>
137 =head1 COPYRIGHT AND LICENSE
139 Copyright 2006-2008 by Infinity Interactive, Inc.
141 L<http://www.iinteractive.com>
143 This library is free software; you can redistribute it and/or modify
144 it under the same terms as Perl itself.