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

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.
  • Comment on Re^2: Answer: Easy way to list prime numbers 1 to 100

Replies are listed 'Best First'.
Re^3: Answer: Easy way to list prime numbers 1 to 100
by gellyfish (Monsignor) on May 23, 2006 at 09:42 UTC

    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.

        Totally in agreement, I got hung up on the push thing and had censored grep from the front of my mind for some reason.

        I'm not so sure about the inefficiency or otherwise of the idiom though, whilst it's pretty pointless here admittedly, we have have had do {} while and do {} until basically forever and documented as upside down loops, and discarding the (implicit) return values is something we do all the time with subroutines and so forth.

        /J\