Using functional tools

Patrick W quitelikely at yahoo.com.au
Sat May 4 14:20:57 EDT 2002


Ken Seehof <kseehof at neuralintegrator.com> writes:

> Okay, if you find obfuscation by functional programming
> as entertaining as I do, try this pattern:
> 
> >>> def f(x):
> ...    return x*10
> ...
> >>> n = 3
> >>> a = [1,2,3,4,5,6,7,8,9]
> >>> [((i+1)%n) and a[i] or f(a[i]) for i in range(len(a))]
> [1, 2, 30, 4, 5, 60, 7, 8, 90]

Here's one without 'for', 'while', list comprehensions, index
manipulations, or any semblance of clarity whatsoever:

def mapn(fun, seq, n, i=0):
    if i == len(seq): return []
    return [[lambda x: x, fun][i%n==0](seq[i])] + mapn(seq,fun,n,i+1)




More information about the Python-list mailing list