CombatSquirrel has asked for the wisdom of the Perl Monks concerning the following question:
where @solution holds the beginning (for example ( 23, 15, 21 ) ) of the solution sequence, @availableIntervals the number of allowed uses left for each possible interval (i.e. the difference between two consecutive notes) and &nmap is a function which works quite similar to map. It is defined as follows:for my $nnot ( map { $solution[-1] + $_, $solution[-1] - $_ } nmap { $_ if ($availableIntervals[$_] > 0) } (1 .. 11) ) {
The sense is that the interval is just used if it can be used. This loop seems terribly awkward to me, especially in a Perl program. Is there any better way of doing the iteration (possibly without using map at all)?sub nmap(&@){ my $rout = shift; # get mapping routine my @list = (); # output list my $curr; # current result for (@_) { $curr = $rout->($_); # get result if (defined $curr) { # if $curr is defined push @list, $curr; # add it to the list } } return @list; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Map function that does not return undefined values
by broquaint (Abbot) on May 13, 2003 at 14:35 UTC | |
by CombatSquirrel (Hermit) on May 13, 2003 at 14:43 UTC | |
|
Re: Map function that does not return undefined values
by jdporter (Paladin) on May 13, 2003 at 14:41 UTC | |
|
Re: Map function that does not return undefined values
by suaveant (Parson) on May 13, 2003 at 14:41 UTC |