in reply to subroutine concatenation
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.)#! perl -l my $f = sub { "f($_[0])" }; my $g = sub { "g($_[0])" }; print $f->("x"); # f(x) print $g->("x"); # g(x)
Now we can compose the functions we defined earlier.sub compose2 { my ($f, $g) = @_; sub { $f->( $g->(@_) ) } }
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.)my $h = compose2( $f, $g ); print $h->("x"); # f(g(x))
Let's give it a try.sub foldl { my $f = shift; my $z = shift; $z = $f->($z, $_) for @_; $z; } sub compose { foldl( \&compose2, @_ ) }
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.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
Cheers,
Tom
Tom Moertel : Blog / Talks / CPAN / LectroTest / PXSL / Coffee / Movie Rating Decoder
|
|---|