Lambda question

Terry Reedy tjreedy at udel.edu
Sun Jun 5 14:33:42 EDT 2011


On 6/5/2011 5:31 AM, Alain Ketterlin wrote:
> <jyoung79 at 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




More information about the Python-list mailing list