Hi everybody, This is my first post to python-dev and mailman told me to introduce myself... I'm computer science grad student at UC Irvine and I've been programming in python for quite some time now. I'm originally from Germany where I studied math together with Marc-Andre Lemburg, who should be somewhat known on this list. ;-) I'd like to advocate the inclusion of efficient (ie iterator-based) predicates to the standard library. If that's asking too much :-) then consider this just a suggestion for updating the documentation of itertools. My reasoning is that these predicate should be used in many places, especially as part of assert statements. IMHO lowering the burden to use assert statements is always a good idea. The examples given in itertools' documentation are a good starting point. More specifically I'm talking about the following: def all(pred, seq): "Returns True if pred(x) is True for every element in the iterable" return False not in imap(pred, seq) def some(pred, seq): "Returns True if pred(x) is True at least one element in the iterable" return True in imap(pred, seq) def no(pred, seq): "Returns True if pred(x) is False for every element in the iterable" return True not in imap(pred, seq) But before including these functions, I would like to propose two changes. 1. Meaning of None as predicate The meaning of None as pred. The above definitions use itertools.imap's interpretation of None as pred arguments, ie None is interpreted as the functions that returns a tuple of its arguments. Therefore all(None, <any-sequence>) will always return True. Similar reasoning renders None as pred useless for some() and no(). I would like to propose pred=None's meaning to be the same as for itertools.ifilter, ie None is interpreted as the identity function, which--in this context--is the same as the bool() function. Now all(None, seq) it true iff all of seq's elements are interpreted as True by bool(). This is potentially valuable information. ;-) 2. Argument order Now that there's a useful default meaning for pred, we should give it a default and make it an optional argument. For this the order of arguments must be reversed. This is different from itertools consistent use of iterables as last arguments. I don't know if this is relvant here. Anyway, since predicates are in general more useful like this I think it's the better choice. So, I propose an implementation like this: def all(seq, pred=bool): return False not in imap(pred, seq) def some(seq, pred=bool): return True in imap(pred, seq) def no(seq, pred=bool): return True not in imap(pred, seq) [ You can see now that the meaning of pred == None was just a strawman. ;-) ] For enhanced assert support I'd advocate additional predicates for easy and fast type checking, eg allListType, allIntType, etc. Maybe all this should go into it's own `preds' module? -- Chris Stork <><><><><><><><><><><><><> http://www.ics.uci.edu/~cstork/ OpenPGP fingerprint: B08B 602C C806 C492 D069 021E 41F3 8C8D 50F9 CA2F
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4