map, for, and while

J

J Krugman

With map, one can streamline for-loops like this one:

my @array_2;
push @array_2, some_sub($_) for @array_1;

into a single assignment:

my @array_2 = map some_sub($_), @array_1;

Is there a way (not necessarily using map) to streamline while-loops
like this one:

my @array;
push @array, some_other_sub() while some_condition();

into a single assignment to @array?

TIA!

jill
 
G

Gunnar Hjalmarsson

J said:
With map, one can streamline for-loops like this one:

my @array_2;
push @array_2, some_sub($_) for @array_1;

into a single assignment:

my @array_2 = map some_sub($_), @array_1;

Is there a way (not necessarily using map) to streamline
while-loops like this one:

my @array;
push @array, some_other_sub() while some_condition();

into a single assignment to @array?

How about using map()?

my @array = map some_sub($_), <FH>;
 
B

Brian McCauley

J Krugman said:
With map, one can streamline for-loops like this one:

my @array_2;
push @array_2, some_sub($_) for @array_1;

into a single assignment:

my @array_2 = map some_sub($_), @array_1;

Is there a way (not necessarily using map) to streamline while-loops
like this one:

my @array;
push @array, some_other_sub() while some_condition();

into a single assignment to @array?

I don't think you'll find a way that's more efficient.

Anyhow, the subroutine-call overhead (let alone whatever's in the
subroutines) is likely to be the largest part of the time so there's
no point worrying about finding a better way to concatenate the return
values from some_other_sub().

#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;

my $i;
sub some_other_sub { 1 }
sub some_condition { $i-- }

timethese(1000,{
subs => sub {
my @array;
$i = 1000;
push @array, some_other_sub() while some_condition();
},
nosubs => sub {
my @array;
$i = 1000;
push @array, 1 while $i--;
},
});
__END__
Benchmark: timing 1000 iterations of nosubs, subs...
nosubs: 1 wallclock secs ( 0.76 usr + 0.00 sys = 0.76 CPU) @ 1315.79/s (n=1000)
subs: 3 wallclock secs ( 2.88 usr + 0.00 sys = 2.88 CPU) @ 347.22/s (n=1000)

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
A

Anno Siegel

Brian McCauley said:
I don't think you'll find a way that's more efficient.

I think it's more convenience than efficiency what Jill(?) is after.
Also, some_other_sub() and some_condition() are probably just examples
for arbitrary expressions.

I guess the idea is the mythical "map ... while", which would allow the
above to be written

map some_other_sub() while some_condition();

(Obviously, "map ... while" is a syntactical construct whose behavior
can't be derived from the normal behavior of "map" and "while".)
Anyhow, the subroutine-call overhead (let alone whatever's in the

[snip benchmark which compared these:]
push @array, some_other_sub() while some_condition();
push @array, 1 while $i--;

my @array = map 1 while $i--;

That would beat them all :)

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top