Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'pypi': 0.04; 'slices': 0.07; 'terry': 0.07; 'python': 0.08; '*and*': 0.09; 'assert': 0.09; 'function:': 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; 'recursion': 0.09; 'successive': 0.09; 'tail': 0.09; 'valueerror:': 0.09; 'output': 0.11; 'def': 0.12; 'written': 0.14; 'am,': 0.14; 'wrote:': 0.14; "'n'": 0.16; 'arg': 0.16; 'docstring': 0.16; 'input,': 0.16; 'into.': 0.16; 'lambda': 0.16; 'n):': 0.16; 'reedy': 0.16; 'succinct': 0.16; 'argument': 0.16; 'yield': 0.19; 'jan': 0.20; 'header:In-Reply- To:1': 0.21; 'code.': 0.22; 'subject:question': 0.23; 'correct,': 0.23; 'null': 0.23; 'welcome.': 0.23; 'code': 0.24; 'function': 0.25; 'statement': 0.26; 'tests': 0.26; 'skip:[ 10': 0.26; 'definition': 0.26; 'thanks': 0.28; 'raise': 0.28; 'problem': 0.28; 'expressions': 0.29; 'good.': 0.29; 'cases.': 0.30; 'i/o': 0.30; 'ago': 0.31; 'this.': 0.31; 'clearly': 0.32; 'cases': 0.32; 'expression': 0.32; 'header:X-Complaints-To:1': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'post': 0.33; 'error': 0.33; 'thank': 0.35; 'example,': 0.35; 'header:User-Agent:1': 0.35; 'fails': 0.35; 'sense,': 0.35; 'else': 0.35; 'test': 0.35; 'something': 0.37; 'change': 0.37; 'designing': 0.37; 'ways': 0.37; 'think': 0.38; 'received:org': 0.38; 'could': 0.38; 'positive': 0.38; 'third': 0.38; 'subject:: ': 0.38; 'doing': 0.39; 'should': 0.39; 'said': 0.39; 'it!': 0.39; 'header:Mime- Version:1': 0.39; 'got': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'everyone': 0.40; 'really': 0.40; 'help': 0.40; 'more': 0.60; 'your': 0.60; 'year': 0.61; 'back': 0.63; 'taking': 0.64; 'incredible': 0.65; 'plus': 0.65; 'here': 0.66; 'share': 0.67; 'accepts': 0.68; 'safe': 0.69; 'packing': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Lambda question Date: Mon, 06 Jun 2011 12:52:31 -0400 References: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> 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: 79 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307379171 news.xs4all.nl 49046 [::ffff:82.94.164.166]:43590 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7095 On 6/6/2011 9:42 AM, jyoung79@kc.rr.com wrote: >>>>>> f =3D lambda x, n, acc=3D[]: f(x[n:], n, acc+[(x[:n])]) if x else = acc > >> Packing tail recursion into one line is bad for both understanding and= >> refactoring. Use better names and a docstring gives >> >> def group(seq, n): >> 'Yield from seq successive disjoint slices of length n plus the >> remainder' >> for i in range(0,len(seq), n): >> yield seq[i:i+n] [I added back the last 'n' that got deleted somehow] > Thank you all very much for this incredible help! The original code > now makes sense, and I was thrilled to see better and more efficient > ways of doing this. Thanks for taking the time to share your > thoughts as well as the excellent detail everyone shared=E2=80=A6 I re= ally > appreciate it! You are welcome. Let me add something not said much here about designing functions: start = with both a clear and succinct definition *and* test cases. (I only=20 started writing tests first a year ago or so.) Test cases help test the=20 definition statement as well as the yet-to-be-written code. They also=20 make re-factoring much safer. I think test cases should start with null=20 inputs. For this function: for inn,out in ( (('',1), []), # no input, no output (('abc',0), []), # definition unclear, could be error (('abc',1), ['a','b','c']), (('abcd',2), ['ab','cd']), (('abcde',2), ['ab', 'cd', 'e']), # could change this ): assert list(group(*inn)) =3D=3D out, (inn,out) This fails with ValueError: range() arg 3 must not be zero I will let you think about and try out what the original code 'f=3D../=20 does with n=3D0. It is not good. A third problem with lambda expressions = is no test for bad inputs. They were added to Python for situations=20 where one needs a function as an argument and and the return expression=20 is self-explanatory, clearly correct, and safe for any inputs it could=20 get in the context it is passed into. For example, lambda x: 2*x. This works: def group(seq, n): 'Yield from seq successive disjoint slices of length n & the remainder= ' if n<=3D0: raise ValueError('group size must be positive') for i in range(0,len(seq), n): yield seq[i:i+n] for inn,out in ( (('',1), []), # no input, no output #(('abc',0), ValueError), # group size positive (('abc',1), ['a','b','c']), (('abcd',2), ['ab','cd']), (('abcde',2), ['ab', 'cd', 'e']), # could change this ): assert list(group(*inn)) =3D=3D out, (inn,out) I have written a function test function that I will post or upload to=20 PyPI sometime. It accepts i/o pairs with error 'outputs', like the one=20 commented out above. --=20 Terry Jan Reedy