map is useless!

Lie Ryan lie.1296 at gmail.com
Sun Jun 6 15:59:02 EDT 2010


On 06/07/10 05:54, D'Arcy J.M. Cain wrote:
> On Mon, 07 Jun 2010 05:27:43 +1000
> Lie Ryan <lie.1296 at gmail.com> wrote:
>> In the most naive uses, map appears to have no advantage over list
>> comprehension; but one thing that map can do that list comprehension
>> still can't do without a walk around the park:
>>
>> def foo(func, args):
>>     g = lambda x: x+1
>>     return [func(g, x) for x in args]
>>
>> foo(map, [[4, 6, 3], [6, 3, 2], [1, 3, 5]])
> 
> foo = lambda x: [y + 1 for y in x]
> [foo(x) for x in [[4, 6, 3], [6, 3, 2], [1, 3, 5]]]
> 
> Didn't seem like such a long walk.
> 

that's because you're simplifying the problem, the correct walk is:

def foo(func, args):
    g = lambda x: x+1
    return [func(g, x) for x in args]

foo((lambda g, a: [g(x) for x in a]), [[4, 6, 3], [6, 3, 2], [1, 3, 5]])



More information about the Python-list mailing list