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


In reply to Re: subroutine concatenation by tmoertel
in thread subroutine concatenation by imcsk8

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.