Re: best cumulative sum



David Isaac wrote:

> def ireduce(func, iterable, init=None):
> if init is None:
> iterable = iter(iterable)
> init = iterable.next()
> yield init
> elif not iterable:

You are in for a surprise here:

>>> def empty():
.... for item in []:
.... yield item
....
>>> bool(empty())
True

>>> bool(iter([]))
True # python 2.3 and probably 2.5

>>> bool(iter([]))
False # python 2.4

> yield init
> for item in iterable:
> init = func(init, item)
> yield init

Peter

.



Relevant Pages

  • Re: Hops and bittering algorithms
    ... Here are the current hops utilization formulae that I have specs for. ... gravity is 1.050 or lower. ... def utilization: ... elif minutes <= 15: ...
    (rec.crafts.brewing)
  • Re: A Revised Rational Proposal
    ... * Binary operators with one Rational operand and one float or Decimal ... if not isinstance(denominator, (int, long)): ... def __repr__: ... elif isinstance): ...
    (comp.lang.python)
  • Re: A Revised Rational Proposal
    ... an int or a long can be converted to a Rational ... Rationals can be implicity or explicity ... def __repr__: ... elif isinstance): ...
    (comp.lang.python)
  • Re: for-else
    ... def foo0(): print 'foo0' ... def foo1(): print 'foo1' ... def bar1(): print 'bar1' ...
    (comp.lang.python)
  • Re: What about a series type?
    ... On 6/7/07, Robert Dober wrote: ... > def initialize ... > @init = init ... puts s1.get_some ...
    (comp.lang.ruby)