On Nov 8, 1:17 pm, Simon King <simon.k...@uni-jena.de> wrote: > Hi Volker, > > On 2012-11-08, Volker Braun <vbraun.n...@gmail.com> wrote: > > > But then you'd always have to write a lot of > > boilerplate to return an iterator. Its much easier to use yield, but then > > you can't return any additional information about finiteness. > > No? What's wrong with __len__?
There are iterators where you do know the result is finite but where it is hard to predict how many items will be returned, e.g. ( x for x in range(1000) if expensive_predicate(x) ). It would be silly to run through this iterable twice: once to get the length and establish it's finite and once to actually sum the entries. Would you just test for the existence of __len__ ? [On this example it actually fails because a generator object doesn't have a __len__ attribute. Do additive semigroups actually need a "sum" method? Is the idea that it can implement a sum strategy that is appropriate for the semigroup? If there were an "iterator generating elements of the given semigroup" type, I could see how having this could be more efficient, but given that we have no guarantee, we have to dispatch through the generic coercion framework for __add__ anyway. That means you might as well just do target_semigroup(reduce(operator.add, <iterator>, ZZ(0))) since you don't have control over the intermediate parents anyway. (ZZ should coerce into any additive semigroup). The above is better expressed as a generic function than as a method on a parent class. -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To post to this group, send email to sage-devel@googlegroups.com. To unsubscribe from this group, send email to sage-devel+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel?hl=en.