This post originated from an RSS feed registered with Python Buzz
by Thomas Guest.
Original Post: You wait all day for a bus…
Feed Title: Word Aligned: Category Python
Feed URL: http://feeds.feedburner.com/WordAlignedCategoryPython
Feed Description: Dynamic languages in general. Python in particular. The adventures of a space sensitive programmer.
Any and all didn’t appear in Python until version 2.5, released in 2006, when the language was already well into its teens.
Why the delay in offering such fundamental functions? An oversight? Or simply that they’re so easy to implement they weren’t thought necessary. Either way, they’re here now.
The functions are closely related and complementary. We can define any in terms of all and vice-versa.
def any_(xs):
return not all(map(operator.not_, xs))
def all_(xs):
return not any(map(operator.not_, xs))
C++ reached its 30s before introducing its own versions of these logical algorithms, any_of and all_of, but made up for lost time by finding room for a third, none_of, which is not any_of.
template <class Iter, class Pred>
bool none_of_(Iter b, Iter e, Pred p)
{
return std::find_if(b, e, p) == e;
}
template <class Iter, class Pred>
bool any_of_(Iter b, Iter e, Pred p)
{
return !none_of_(b, e, p);
}
template <class Iter, class Pred>
bool all_of_(Iter b, Iter e, Pred p)
{
return !any_of_(b, e, std::not1(p));
}