Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Subroutine chaining idiom requested

by princepawn (Parson)
on May 24, 2001 at 06:35 UTC ( #82795=perlquestion: print w/ replies, xml ) Need Help??
princepawn has asked for the wisdom of the Perl Monks concerning the following question:

There was a near article in about Issue 19 of the Perl Journal called "Design Patterns" in which this guy had a neat one-liner. Let's say we have;
$agenda = [ \a, \b, \c ];
where a, b, c are subs which expect and return arrays.

He wrote a one-liner which ran through the subs, taking the output of one and sending it in as the input of the next sub.

Comment on Subroutine chaining idiom requested
Download Code
Re: Subroutine chaining idiom requested
by premchai21 (Curate) on May 24, 2001 at 06:49 UTC
    Hmmm... I've never read that, but to just construct one on my own:
    $agenda = [ sub { "Hello" }, sub { scalar reverse shift }, sub { chop +shift } ]; chain($agenda, [ ]); ### BEGIN GOLF MODE ### sub chain($$) { my@x=@{+pop};foreach(@{+shift}){@x=$_->(@x)}@x } ### END GOLF MODE ###

    Update: Fixed argument-modification error, thanks to tilly for pointing that out.

      Did someone say golf?
      sub chain{(*f,*_)=@_;@_=&$_ for@f;@_}
      Of course you probably want strict compliant and it is bad style to modify your arguments in place. In which case it is probably better to make that:
      sub chain{my($f,$a)=@_;$a=[&$_(@$a)]for@$f;@$a}
      which is still reasonable...
Re: Subroutine chaining idiom requested
by tachyon (Chancellor) on May 24, 2001 at 09:05 UTC
    Here's mine, 34 chars for the task which did not specify a print requirement. 32 without the my for strict compliance. 27 if you use something shorter than $agenda like $a

    tachyon

    use strict; # a ref to a few subrefs to go, performing as secified # in that they take an array, do someting and return an array my $agenda = [ sub{my@in=@_;for(@in){$_=$_*2}@in;}, sub{my@in=@_;for(@in){$_=$_*2}@in;}, sub{my@in=@_;for(@in){$_=$_*2}@in;}, ]; # four my@a=(1,2,3);@a=&$_(@a)for@$agenda;print"@a";

    Edited 2001-05-24 by Ovid

Re: Subroutine chaining idiom requested
by merlyn (Sage) on May 24, 2001 at 09:23 UTC
Re: Subroutine chaining idiom requested
by tachyon (Chancellor) on May 24, 2001 at 09:32 UTC

    A few more variations on the theme

    tachyon

    use strict; # a few subrefs to go, performing as specified my $agenda = [ sub{my@in=@_;for(@in){$_=$_*2}@in;}, sub{my@in=@_;for(@in){$_=$_*2}@in;}, sub{my@in=@_;for(@in){$_=$_*2}@in;}, ]; # here is a little sub of 39 chars print chain(1,2,3,$agenda); sub chain{ my$a=pop; @_=&$_(@_)for@$a; @_ } # here it is as an anonymous sub print&{sub{my$a=pop;@_=&$_(@_)for@$a;@_}}(1,2,3,$agenda);
Re: Subroutine chaining idiom requested
by FouRPlaY (Monk) on May 24, 2001 at 20:57 UTC
    Is it just me, or is everything on this site turning into a golf challenge? =)



    FouRPlaY
    Learning Perl or Going To die() Trying

      FouRPlaY wrote:

      Is it just me, or is everything on this site turning into a golf challenge? =)

      78 letters. My solution:

      Imagining? Does all here become golf?

      37 letters. Though with English, one might argue that I've altered the meaning.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        35 characters. :-)
        Imagining? Turns all here to golf?
        And restucturing produces the 29 character logical equivalent of:
        All here to golf or me only.
        I don't think that can be improved much. :-)
Re: Subroutine chaining idiom requested
by Anonymous Monk on May 25, 2001 at 10:55 UTC

    28 chars and parses under strict

    use strict; 'I',? you.Is all here->GOLF?

    tachyon

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://82795]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-12-25 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls