Re: Subroutine chaining idiom requested
by merlyn (Sage) on May 24, 2001 at 09:23 UTC
|
use List::Util qw(reduce); # unnecessary in in Perl 6 {grin}
my @result = reduce { [$b->{@$a}] } [], @$agenda;
Or, without that:
my @result;
@result = $_->(@result) for @$agenda;
-- Randal L. Schwartz, Perl hacker | [reply] [Watch: Dir/Any] [d/l] [select] |
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. | [reply] [Watch: Dir/Any] [d/l] |
|
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... | [reply] [Watch: Dir/Any] [d/l] [select] |
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 | [reply] [Watch: Dir/Any] [d/l] |
Re: Subroutine chaining idiom requested
by tachyon (Chancellor) on May 24, 2001 at 09:32 UTC
|
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);
| [reply] [Watch: Dir/Any] [d/l] |
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
| [reply] [Watch: Dir/Any] |
|
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.
| [reply] [Watch: Dir/Any] |
|
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. :-)
| [reply] [Watch: Dir/Any] |
|
|
|
|
Re: Subroutine chaining idiom requested
by Anonymous Monk on May 25, 2001 at 10:55 UTC
|
use strict;
'I',? you.Is all here->GOLF?
tachyon | [reply] [Watch: Dir/Any] [d/l] |