What is the "functional" way of doing this?

Alexander Schmolck a.schmolck at gmail.com
Wed Aug 1 12:01:39 EDT 2007


beginner <zyzhu2000 at gmail.com> writes:

> Hi,
>
> If I have a number n and want to generate a list based on like the
> following:
>
> def f(n):
>      l=[]
>      while n>0:
>          l.append(n%26)
>          n /=26
>     return l
>
> I am wondering what is the 'functional' way to do the same.

This is very 'functional' (and also quite concise):

f = compose(list,partial(unfold, divmod(_,26)))

The definitions of compose, unfold, and _ are left as excercises (of
increasing difficulty) for the reader.

'as



More information about the Python-list mailing list