[Melbourne-pm] I <3 map & grep too :)

Toby Corkindale toby.corkindale at strategicdata.com.au
Wed Oct 26 23:22:11 PDT 2011


On 27/10/11 17:12, Sam Watkins wrote:
> On Thu, Oct 27, 2011 at 04:07:45PM +1100, Toby Corkindale wrote:
>> But perhaps, a little too much..
>> I just found myself this:
>>
>>
>>      use Config::General qw(ParseConfig);
>>      ...
>>      opendir(my $dir, $self->sites_dir) or die..;
>>
>>      my %sites = map  { $_->{name} =>  Streuth::Site->new($_) }
>>                  map  { { ParseConfig($_) } }
>>                  grep { -f $_ }
>>                  map  { File::Spec->catfile($self->sites_dir, $_) }
>>                  grep { /^[-_a-zA-Z\d\.]+$/ }
>>                  readdir($dir);
>>
>>      closedir $dir;
>>      return \%sites;
>
> Looks good to me :)
>
> I usually read map/grep chains in perl from the bottom up,
> maybe because I'm accustomed to the shell's way of doing it.
>
> I've been reading about 'flow based programming', your snippet would
> fits nicely in that data-flow style of programming (or the functional style).
>
> An advantage of map / grep / reduce, compared to 'for', is that a program like
> this could be run in parallel, in two different ways.  It could operate on
> multiple data in parallel (shell doesn't normally do that), and it could use
> 'pipelining' with all the steps running in parallel (shell does do that).


And indeed, I picked up this style of programming from Scala, which does 
do those operations in parallel - in both senses.
It can start running the later operations before the early ones have 
finished - and it can use multiple CPUs to process each stage in parallel.

It's pretty nifty, and I hope Perl gets functionality like that one day 
too..

Toby


More information about the Melbourne-pm mailing list