Widely useful Python utilities by Peter Norvig
aima.cs.berkeley.edu
aima.cs.berkeley.edu
`every` and `some` can be emulated by composing `map` (or a listcomp) with the builtin `any` and `all` (which, for some reason, don't take an optional predicate)
No they can't - this version wouldn't short circuit evaluation, which is completely the wrong behavior. Map would have to apply the predicate to every element of the list before any/all kicked in.
As a trivial example, consider I had a million item list and want to find out if every element is prime. If 8 is the first element, 'every' would return false after looking at the first item - while your composition would apply the prime predicate to all million elements (which would be ridiculously slow ...) before even starting to apply 'all' (which would short circuit, after seeing the first false).
http://www.python.org/dev/peps/pep-0289/
big_list = make_big_list()
if any( not prime( a ) for a in big_list ) :
do_thing()
When you surround the list comprehension syntax with parenthesis instead of square brackets, either as a lone argument to a function or independently, you create a generator that will iterate once over the given data.This allows for functions accepting sequences to short circuit without completing the function mapping.
Just use the lazy map itertools.imap (or Python 3, where `map` is lazy) or replace listcomps by gencomps, any & all take any iterable if you're worried about that. Stop creating problems where there is none.
This kills me pretty much every time I use those functions. It seems like 9 times out of 10, I need the ability to pass in a predicate function.
This always struck me as a major oversight... does anyone know if this design decision was made for a particular reason?
Besides, I guess if `any` had a signature like
def any(xs, pred=lambda x: bool(x))
or something, that would reverse the signatures of map, filter, reduce, etc.On that note, I always want the arguments of filter and reduce, but not those of map, to be reversed for some reason.
all(filter(f, items))
Composing calls like this looks cleaner than incorporating into the function interface all list manipulations that the implementor expects you to want.It's just that my first instinct is always to use all(xs, pred).
A simple example:
students_by_grade = inverse_image(students, lambda s: s.grade)
Then students_by_gender["A"] contains a list of all the A students (and so on).http://code.google.com/p/aima-python/source/browse/#svn/trun...
excellent. and great comment: """This is Tom's personal Python subroutine library. You are not expected to understand this.
Warning: this file contains HIGH MAGIC and DEEP VOODOO, as well as quite a bit of QUESTIONABLE CODE and very few COMMENTS. Use with caution.
ha!
i'm personally very cautious about introducing "magic" and "voodoo" into my repositories. i many times have to tweak libraries (add features, fix bugs), so it's worth them making sense without me having to refactor them first (i did this with qunit, tho...it's hard to generalize, eh?).
more importantly, it's important to me that new developers ramp up fast. there's something about collaboration that many lone programmers don't grok. similarly, the code must make sense to me next month. extensibility and maintainability are almost inversely related to voodoo and magic.
good abstraction, handling complexity through elegance, now these are worth having.
it can be quite hard separating good programming from bad. it is even harder when the work itself is complex in nature, although that is when good programming really shines.
infinity = 1.0e400
>>> 1.0e400 == float("inf")
True
I don't know, if there is a difference. Maybe the interpreter can evaluate 1.0e400 a little faster?