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


Groups > comp.lang.python > #7054

Re: Lambda question

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'else:': 0.03; 'decent': 0.07; 'slice': 0.07; 'slices': 0.07; 'terry': 0.07; 'typed': 0.07; 'attribute': 0.09; 'lost.': 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; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'alain': 0.16; 'chunks': 0.16; 'docstring': 0.16; 'handy': 0.16; 'lambda': 0.16; 'n):': 0.16; 'read:': 0.16; 'reedy': 0.16; 'sequence.': 0.16; 'slicing': 0.16; 'unneeded': 0.16; 'repeated': 0.19; 'rewrite': 0.19; 'yield': 0.19; 'writes:': 0.19; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; '(but': 0.22; 'subject:question': 0.23; 'trying': 0.23; 'translated': 0.23; 'code': 0.24; "doesn't": 0.25; 'index': 0.25; '(and': 0.25; 'moving': 0.26; 'object': 0.26; "i'm": 0.27; 'skip:p 30': 0.28; 'problem': 0.28; 'solving': 0.29; 'instead': 0.29; 'code,': 0.29; 'characters,': 0.30; 'looks': 0.31; 'equivalent': 0.31; 'header:X -Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; 'familiar': 0.33; 'list': 0.33; '...': 0.34; 'header:User-Agent:1': 0.35; 'latter': 0.35; 'couple': 0.35; 'else': 0.35; 'using': 0.35; 'several': 0.36; 'certain': 0.36; 'idea': 0.36; 'useful': 0.37; 'sequence': 0.37; 'think': 0.38; 'received:org': 0.38; 'could': 0.38; 'problem.': 0.38; 'but': 0.38; 'purposes': 0.38; 'returning': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'easier': 0.39; 'editor': 0.39; 'header:Mime-Version:1': 0.39; 'list,': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'missing': 0.40; 'totally': 0.40; 'easily': 0.60; 'more': 0.60; 'huge': 0.62; 'grab': 0.63; 'plus': 0.65; 'spaces': 0.73; 'unnecessary': 0.73; 'saving': 0.74; 'clipping': 0.84; 'dense': 0.84; 'inferior': 0.84; 'packing': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Lambda question
Date Sun, 05 Jun 2011 14:33:42 -0400
References <mailman.2454.1307209587.9059.python-list@python.org> <87fwnor5dd.fsf@dpt-info.u-strasbg.fr>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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 <87fwnor5dd.fsf@dpt-info.u-strasbg.fr>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2471.1307298836.9059.python-list@python.org> (permalink)
Lines 83
NNTP-Posting-Host 82.94.164.166
X-Trace 1307298836 news.xs4all.nl 49176 [::ffff:82.94.164.166]:50385
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7054

Show key headers only | View raw


On 6/5/2011 5:31 AM, Alain Ketterlin wrote:
> <jyoung79@kc.rr.com>  writes:
>
>>>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc

f=lambda ... statements are inferior for practical purposes to the 
equivalent def f statements because the resulting object is missing a 
useful name attribute and a docstring. f=lambda is only useful for 
saving a couple of characters, and the above has many unneeded spaces

>>>>> f("Hallo Welt", 3)
>> ['Hal', 'lo ', 'Wel', 't']
>>
>> http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-s
>> ized-chunks-in-python/312644
>>
>> It doesn't work with a huge list, but looks like it could be handy in certain
>> circumstances.  I'm trying to understand this code, but am totally lost.
>
> With such dense code, it is a good idea to rewrite the code using some
> more familiar (but equivalent) constructions. In that case:
>
> f =<a function that can be called with parameters>  x, n, acc=[]:
>        <if>  x<is not empty>
>          <result-is>  f(x[n:], n, acc+[(x[:n])])
>        <else>
>          <result-is>  acc

Yes, the following is much easier to read:

def f(x, n, acc=[]):
   if x:
     return f(x[n:], n, acc + [x[:n]])
   else:
     return acc

And it can be easily translated to:

def f(x,n):
   acc = []
   while x:
     acc.append(x[:n])  # grab first n chars
     x = x[n:]          # before clipping x
   return acc

The repeated rebinding of x is the obvious problem. Returning a list 
instead of yielding chunks is unnecessary and a problem with large 
inputs. Solving the latter simplies the code to:

def group(x,n):
   while x:
     yield x[:n]  # grab first n chars
     x = x[n:]    # before clipping x

print(list(group('abcdefghik',3)))
# ['abc', 'def', 'ghi', 'k']

Now we can think about slicing chunks out of the sequence by moving the 
slice index instead of slicing and rebinding the sequence.

def f(x,n):
     for i in range(0,len(x),n):
         yield x[i:i+n]

This is *more* useful that the original f= above and has several *fewer* 
typed characters, even is not all on one line (and decent editor add the 
indents automatically):

def f(x,n): for i in range(0,len(x),n): yield x[i:i+n]
f = lambda x, n, acc=[]: 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+]

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Lambda question <jyoung79@kc.rr.com> - 2011-06-04 17:46 +0000
  Re: Lambda question Mel <mwilson@the-wire.com> - 2011-06-04 14:21 -0400
  Re: Lambda question Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-06-05 11:31 +0200
    Re: Lambda question Terry Reedy <tjreedy@udel.edu> - 2011-06-05 14:33 -0400
      Re: Lambda question rusi <rustompmody@gmail.com> - 2011-06-06 10:29 -0700
        Re: Lambda question Terry Reedy <tjreedy@udel.edu> - 2011-06-06 21:56 -0400

csiph-web