Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Answer: Easy way to list prime numbers 1 to 100

by blazar (Canon)
on May 22, 2006 at 11:01 UTC ( [id://550903]=note: print w/replies, xml ) Need Help??


in reply to Re: Easy way to list prime numbers 1 to 100
in thread Easy way to list prime numbers 1 to 100

In fact you seem to be calculating all of the odd numbers rather than the prime numbers.

Indeed, and in a very clumsy way, including the deprecated use of a map in void context...

Update: at the moment of this update this node has gained a reputation of -4. So far so fine. However this should imply that at least four persons did disagree with my claim. I must deduce that those persons think that

@prime = (1,2); map { push(@prime,$_) if (($_ % 2) != 0) } (2 .. 100);
is a very nice, clean and elegant way to obtain the list of "primes" (in l.frankline's arithmetic) as opposed to, say,
my @prime=(1,2,grep $_%2, 2..100);

PS: from perldoc perlstyle:

Avoid using grep() (or map()) or `backticks` in a void context, that is, when you just throw away their return values. Those functions all have return values, so use them. Otherwise use a foreach() loop or the system() function instead.

Replies are listed 'Best First'.
Re^2: Answer: Easy way to list prime numbers 1 to 100
by BrowserUk (Patriarch) on May 22, 2006 at 21:22 UTC
    including the deprecated use of a map in void context

    Is map in a void context deprecated?

    I thought that the main concern with using map in a void context was the inefficiency of building a results list that is then discarded--but that was corrected in 5.8.1 (from the delta):

    map in void context is no longer expensive. map is now context aware, and will not construct a list if called in void context.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Whilst I agree that deprecated is too strong a word and that it is no longer wasteful to use it in this way, it does seem semantically odd to use map in a void context, which I think has always been one of the major objections to doing this, something like:

      do { push(@prime,$_) if ($_ % 2) } for (2 .. 100);
      is probably closer to being clear (for me at any rate.)

      /J\

        Semantically odd I'd agree with, though that's almost par for the course in Perl :)

        That said, your alternative is possible worse, in as much as you are (repeatedly) calling do in a void context, so unless it is context aware, you're discarding it's return values.

        Ignoring that the code doesn't make much sense, I'd go for

        @odds = grep{ $_ % 2 } 2 .. 100;

        with those small hardcoded limits, or if they were much larger or unknown:

        $_ % 2 and push @odds, $_ for 2 .. $LIMIT;

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found