NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
List::MoreUtils? (Score:2)
While I can't say I'm wild about the name -- though I can't think of a better one -- the List::MoreUtils [cpan.org] module seems to offer a more general solution.
Re: (Score:1)
s/any/grep/and remove theuseline and that works.Re: (Score:2)
Because if you have a large list, grep forces you to iterate over every element. That could be expensive if called many times. any is easy to read and terminates as soon as any element in the list meets the condition. Of course, that still doesn't mean that any is better, but if you're using that in conjunction with other functions in that module, it can be a win.
Re: (Score:1)
Well, yes. And I did consider that.
But it really only matters if you have a large list in which you look for some values much more frequently than for others, so reordering the list can yield consistent savings. Otherwise you’re just reducing the runtime from n steps to n/2 averaged steps – both of which are O(n).
If that tiny an optimization actually manages to make a noticable difference, then you really have no excuse not to use a hash.
If the code block was doing something much more complex that can’t be expressed in terms of hash
exist/lookup,anywould be worth considering, but for a simple ∈ operation, any case too big forgrepto handle is also too big foranyto handle relative to the better option that is available.Reply to This
Parent