Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15114 > unrolled thread

Re: Fast recursive generators?

Started byTerry Reedy <tjreedy@udel.edu>
First post2011-10-28 16:45 -0400
Last post2011-10-28 16:45 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Fast recursive generators? Terry Reedy <tjreedy@udel.edu> - 2011-10-28 16:45 -0400

#15114 — Re: Fast recursive generators?

FromTerry Reedy <tjreedy@udel.edu>
Date2011-10-28 16:45 -0400
SubjectRe: Fast recursive generators?
Message-ID<mailman.2292.1319834744.27778.python-list@python.org>
On 10/28/2011 2:10 PM, Michael McGlothlin wrote:
> I'm trying to generate a list of values

Better to think of a sequence of values, whether materialized as a 
'list' or not.

> where each value is dependent
> on the previous value in the list and this bit of code needs to be
> repeatedly so I'd like it to be fast. It doesn't seem that
> comprehensions will work as each pass needs to take the result of the
> previous pass as it's argument. map() doesn't seem likely. filter() or

Comprehensions combine map and filter, both of which conceptually work 
on each item of a pre-existing list independently. (I am aware that the 
function passed can stash away values to create dependence.

> reduce() seem workable but not very clean. Is there a good way to do
> this? About the best I can get is this:
>
> l = [ func ( start ) ]
> f = lambda a: func ( l[-1] ) or a
> filter ( f, range ( big_number, -1, -1 ) )
>
>
> I guess I'm looking for something more like:
>
> l = do ( lambda a: func ( a ), big_number, start )

Something like

def do(func, N, value):
     yield value
     for i in range(1,N):
         value = func(value)
         yield value

?
For more generality, make func a function of both value and i.
If you need a list, "l = list(do(f,N,x))", but if you do not, you can do 
"for item in do(f,N,x):" and skip making the list.

-- 
Terry Jan Reedy

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web