One powerful way to join functions together is to "compose" them.
What this means is that you glue the functions into a pipeline,
so that one function is called after the other, and the output
of the one becomes the other's input. To see how it works,
let us consider two functions f and g:
#! perl -l
my $f = sub { "f($_[0])" };
my $g = sub { "g($_[0])" };
print $f->("x");
# f(x)
print $g->("x");
# g(x)
Now let us create a simple function compose2 that glues
the two functions together into a pipeline.
(We call it compose2 because it composes 2 functions.)
sub compose2 {
my ($f, $g) = @_;
sub { $f->( $g->(@_) ) }
}
Now we can compose the functions we defined earlier.
my $h = compose2( $f, $g );
print $h->("x");
# f(g(x))
We can extend composition to any number of functions by "folding" our
2-function version compose2 over a list of function
arguments. We will borrow a function foldl from the realm of
functional programming to help us. (This function is very much like
reduce from the List::Util module.)
sub foldl {
my $f = shift;
my $z = shift;
$z = $f->($z, $_) for @_;
$z;
}
sub compose {
foldl( \&compose2, @_ )
}
Let's give it a try.
print compose( $f,$f,$f,$f,$f,$f,$g )->("x");
# f(f(f(f(f(f(g(x)))))))
my $add1 = sub { $_[0] + 1 };
my $add2 = compose( $add1, $add1 );
my $add4 = compose( $add2, $add2 );
print $add2->(0);
# 2
print $add4->(0);
# 4
print compose( $add1, $add1, $add1 )->(0);
# 3
print compose( ($add1) x 4 )->(0);
# 4
Function composition is a simple idea – take two functions and
glue them together – but you can do some surprisingly cool things
with it. (See Re: Ways to implement a closure for more fun with function composition.)
Maybe instead of just concatenating your functions, you might get
more mileage from composing them.
Cheers, Tom
|