Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: multiple consumer of an array

by AppleFritter (Vicar)
on Oct 18, 2015 at 10:29 UTC ( [id://1145254]=note: print w/replies, xml ) Need Help??


in reply to multiple consumer of an array

Sure, this is possible. Here's a quick-and-dirty solution where each processor_ subroutine is called on the same data sequentially:

#!/usr/bin/perl use Modern::Perl '2014'; use List::MoreUtils qw/minmax/; use Data::Dumper; # count odd/even elements sub process_1(@) { my %result = (); foreach my $element (@_) { my $type = ($element % 2) ? "odd" : "even"; $result{$type}++; } return \%result; } # find minimum/maximum value sub process_2(@) { my @minmax = minmax(@_); return \@minmax; } sub compose { my @subs = @_; return sub { my @results = (); # apply each sub in turn and save the result foreach my $sub (@subs) { push @results, &$sub(@_); } return \@results; } } # sample data my @data = (1, 6, 813, 472, 134, 48, 99, 1398, 47); # compose subroutines my $process_all = compose(\&process_1, \&process_2); # apply all subroutines my $result = &$process_all(@data); say Dumper $result;

Is this what you're looking for?

BTW, you may also be interested in Dominus's Higher-Order Perl (also available as a PDF here).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1145254]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found