Re: Proposed new collection methods

2005-08-07 Thread Terry Reedy
"Mike Meyer" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Another thread pointed out a couple of methods that would be nice to > have on Python collections: find and inject. Since Python does not have a collections superclass, I am puzzled as to what you are really proposing.

Re: Proposed new collection methods

2005-08-07 Thread Christopher Subich
Jeff Schwab wrote: > Robert Kern wrote: >> Now, if I were to do >> >> item = g(self, test).next() >> >> the generator would execute the code until it reached the yield >> statement which happens when it finds the first item that passes the >> test. That item will get returned, and execution doe

Re: Proposed new collection methods

2005-08-06 Thread Christopher Subich
Robert Kern wrote: > Jeff Schwab wrote: >> Why are you retarded? Isn't the above code O(n)? >> >> Forgive me for not understanding, I'm still awfully new to Python >> (having come from Perl & C++), and I didn't see an explanation in the >> FAQ. > (s for s in iter(self) is test(s)) is a generato

Re: Proposed new collection methods

2005-08-06 Thread Jeff Schwab
Robert Kern wrote: > (s for s in iter(self) is test(s)) is a generator expression. It is > roughly equivalent to > > def g(self, test=lambda x: True): > for s in iter(self): > if test(s): > yield s > > Now, if I were to do > > item = g(self, test).next() > > the generato

Re: Proposed new collection methods

2005-08-06 Thread Robert Kern
Jeff Schwab wrote: > Robert Kern wrote: > >>Robert Kern wrote: >> >>>Christopher Subich wrote: >>> Dear Zeus no. Find can be defined as: def find(self, test=lambda x:1): try: item = (s for s in iter(self) if test(s)).next() except StopIteration: raise Val

Re: Proposed new collection methods

2005-08-06 Thread Jeff Schwab
Robert Kern wrote: > Robert Kern wrote: > >> Christopher Subich wrote: >> >> >>> Dear Zeus no. Find can be defined as: >>> def find(self, test=lambda x:1): >>>try: >>> item = (s for s in iter(self) if test(s)).next() >>>except StopIteration: >>> raise ValueError('No matching i

Re: Proposed new collection methods

2005-08-06 Thread Robert Kern
Robert Kern wrote: > Christopher Subich wrote: > > >>Dear Zeus no. Find can be defined as: >>def find(self, test=lambda x:1): >>try: >> item = (s for s in iter(self) if test(s)).next() >>except StopIteration: >> raise ValueError('No matching items in list') > > I would prefe

Re: Proposed new collection methods

2005-08-06 Thread Robert Kern
Christopher Subich wrote: > Dear Zeus no. Find can be defined as: > def find(self, test=lambda x:1): > try: >item = (s for s in iter(self) if test(s)).next() > except StopIteration: >raise ValueError('No matching items in list') I would prefer that a find() operation retu

Re: Proposed new collection methods

2005-08-06 Thread Christopher Subich
Mike Meyer wrote: > Another thread pointed out a couple of methods that would be nice to > have on Python collections: find and inject. These are taken from > http://martinfowler.com/bliki/CollectionClosureMethod.html >. > > find can be defined as: > > def find(self, test = None): >

Proposed new collection methods

2005-08-06 Thread Mike Meyer
Another thread pointed out a couple of methods that would be nice to have on Python collections: find and inject. These are taken from http://martinfowler.com/bliki/CollectionClosureMethod.html >. find can be defined as: def find(self, test = None): for item in self: if