shy stackless Re: ANNOUNCE: xsdb -- the eXtremely Simple Database goes alpha
Duncan Booth
duncan at NOSPAMrcp.co.uk
Thu Dec 11 10:50:07 EST 2003
Jp Calderone <exarkun at intarweb.us> wrote in
news:mailman.68.1071152873.9307.python-list at python.org:
> This works, but it is even easier. All you need is top-level code
> to
> handle it:
>
>
> def unroll(f, *a, **kw):
> gstack = [iter(f(*a, **kw))]
> while gstack:
> try:
> e = gstack[-1].next()
> except StopIteration:
> gstack.pop()
> else:
> if isinstance(e, types.GeneratorType):
> gstack.append(e)
> else:
> yield e
>
>
> def inorder(t):
> if t:
> yield inorder(t.left)
> yield t.label
> yield inorder(t.right)
>
> unroll(inorder, t)
>
>
> A bit more frameworky code, but it's all isolated in one place,
> which is
> much nicer than having to spread it all over the place.
Nice idea, provided you never want to yield a generator. Also should it
check for a generator, or just for any iterator.
You can also go for a recursive definition of unroll and use it to unroll
itself which I think reads a bit more clearly.
def unroll(iterator):
for v in iterator:
if isinstance(v, types.GeneratorType)
for inner in unroll(v): yield inner
else:
yield v
for node in unroll(inorder(t)):
... do whatever ...
I wonder if this is useful enough to go in itertools?
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list