Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip': 0.04; 'received:verizon.net': 0.07; 'terry': 0.07; 'filter,': 0.09; 'func': 0.09; 'likely.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.15; 'lambda': 0.16; 'reedy': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'jan': 0.19; 'this?': 0.21; 'trying': 0.21; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'pm,': 0.24; 'code': 0.25; 'guess': 0.26; "i'm": 0.27; 'function': 0.27; 'repeatedly': 0.28; 'bit': 0.28; 'pass': 0.29; 'yield': 0.29; 'not.': 0.30; '-1,': 0.30; 'clean.': 0.30; 'values,': 0.30; 'subject:?': 0.31; 'seem': 0.31; 'michael': 0.31; 'values': 0.32; 'list': 0.32; 'dependent': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.34; 'filter': 0.34; 'like:': 0.34; 'header:X-Complaints-To:1': 0.35; 'list.': 0.35; 'sequence': 0.37; 'passed': 0.37; 'list,': 0.37; 'but': 0.37; 'something': 0.37; 'not,': 0.38; 'think': 0.38; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; "it's": 0.40; 'where': 0.40; 'more': 0.60; 'making': 0.67; '"for': 0.67; 'pre- existing': 0.84; 'value):': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Fast recursive generators? Date: Fri, 28 Oct 2011 16:45:21 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1319834744 news.xs4all.nl 6921 [2001:888:2000:d::a6]:51801 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15114 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