469,306 Members | 2,519 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Make a generator from a recursive function

This was my answer to the thread "new in programing":

def do_something(*args):
print args

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
do_deeply(first+1, depth+1, lim, False, *args)
if first <= depth:
do_deeply(first+1, depth, lim, True, *args + (first,))
elif doit:
do_something(*args)

do_deeply(first=1, depth=3, lim=4)

I thought it was a good answer, but I think better would be a generator. Is
there a straightforward way to make such a function a generator, or does it
require a not using a recursive function? I think "cheating" would be to
generate the list and make an iterable from it.

Note: this is not the same as "cross" from the "N-uples from list of lists" thread.

James
Dec 10 '05 #1
2 1579
James Stroud <js*****@mbi.ucla.edu> wrote:
...
This was my answer to the thread "new in programing":

def do_something(*args):
print args

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
do_deeply(first+1, depth+1, lim, False, *args)
if first <= depth:
do_deeply(first+1, depth, lim, True, *args + (first,))
elif doit:
do_something(*args)

do_deeply(first=1, depth=3, lim=4)

I thought it was a good answer, but I think better would be a generator. Is
there a straightforward way to make such a function a generator, or does it


I'm not entirely sure what you mean, but I will guess it's something not
too different from...:

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
for x in do_deeply(first+1, depth+1, lim, False, *args):
yield x
if first <= depth:
for x in do_deeply(first+1, depth, lim, True, *args + (first,)):
yield x
elif doit:
yield args

to be used with

for x in do_deeply(first=1, depth=3, lim=4):
do_something(*x)
Did I guess right...?
Alex
Dec 10 '05 #2
Alex Martelli wrote:
James Stroud <js*****@mbi.ucla.edu> wrote:
...
This was my answer to the thread "new in programing":

def do_something(*args):
print args

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
do_deeply(first+1, depth+1, lim, False, *args)
if first <= depth:
do_deeply(first+1, depth, lim, True, *args + (first,))
elif doit:
do_something(*args)

do_deeply(first=1, depth=3, lim=4)

I thought it was a good answer, but I think better would be a generator. Is
there a straightforward way to make such a function a generator, or does it

I'm not entirely sure what you mean, but I will guess it's something not
too different from...:

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
for x in do_deeply(first+1, depth+1, lim, False, *args):
yield x
if first <= depth:
for x in do_deeply(first+1, depth, lim, True, *args + (first,)):
yield x
elif doit:
yield args

to be used with

for x in do_deeply(first=1, depth=3, lim=4):
do_something(*x)
Did I guess right...?
Alex


Yes, that's what I was thinking. Thank you.
Dec 10 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by MetalOne | last post: by
8 posts views Thread by Paul Chiusano | last post: by
2 posts views Thread by Johannes Ahl mann | last post: by
2 posts views Thread by | last post: by
7 posts views Thread by aurora | last post: by
5 posts views Thread by Jerzy Karczmarczuk | last post: by
6 posts views Thread by Mike C# | last post: by
3 posts views Thread by from.future.import | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.