[Tutor] Writing the Map function as a oneliner
Alan Gauld
alan.gauld at btinternet.com
Tue Nov 8 01:27:56 CET 2011
On 07/11/11 23:32, Rafael Turner wrote:
> Hello,
>
> I am trying to write the map function as a oneliner....
> But I would like to make recursive version. Here is what I was thinking:
>
> I can write map as
>
> def pyMap(F,li):
> if li == []:
> return []
> else:
> return [F(li[0])] + map2(F, li[1:])
>
Using the conditional expression it is nearly trivial to
convert your function to a one-liner:
def pyMap(F,li):
return [] if li == [] else [F(li[0])] + pyMap(F,li[1:])
which becomes
pyMap = lambda F, li: [] if li == [] else [F(li[0])] + pyMap(F,li[1:])
HTH,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list