in reply to if ... elsif ... else

With List::Util, you can write either say $i unless any { $i % $_ } (2,3,5); or say $i if all { $i % $_ == 0 } (3,4,5);

Replies are listed 'Best First'.
Re^2: if ... elsif ... else
by AnomalousMonk (Archbishop) on Jun 01, 2017 at 15:41 UTC

    Please note that  any() all() none() etc. are not found in early versions of List::Util. These functions were introduced first in List::MoreUtils and later included in the former module — but they're still present in the latter.


    Give a man a fish:  <%-{-{-{-<

      I did think about checking if they where present in List::Util or not, but didn't think about previous versions. ++ for the precision.

      Anyway, this is a case where any() can be replaced by grep to obtain the same result. So say $i unless grep { $i % $_ } (2,3,5);

        ... any() can be replaced by grep to obtain the same result.

        Insofar as the final result is concerned, I'd agree. The only caveat I'd offer is that for sufficiently large sets of prime factors 2, 3, 5, ..., or of ranges of numbers to be checked, use of a short-circuiting function like  any() or  none() may offer a noticable performance advantage over grep, which will always process every item in its input list. Of course, this can only be finally determined by benchmarking of some kind.


        Give a man a fish:  <%-{-{-{-<