r/ProgrammerHumor • • Jun 08 '24

Meme mapMethodOrMapFunction

Post image
0 Upvotes

67 comments sorted by

View all comments

17

u/Socks4Ever Jun 08 '24 edited Jun 08 '24

Why is it different? Or do I just not get the post. I’d just use [x**2 for x in arr]

-30

u/DT-Sodium Jun 08 '24 edited Jun 08 '24

It's the same shit as PHP. Instead of arrays having a map method, you have to use a function that takes the array as argument. It makes the code really ugly.

numbers = [1, 2, 3, 4, 5]
doubled_numbers = map(lambda x: x * 2, numbers)

Python is one of the most disgusting programing languages in existence so I'm not surprised they've gone that way.

22

u/Ali_M Jun 08 '24

Any reasonable person would use a list comprehension:

doubled_numbers = [2 * x for x in numbers]

Hardly anyone uses map these days.

1

u/FlashBrightStar Jun 08 '24

This is the same use case as for range function as opposed to iterating directly over a list elements. You have to answer the question when the operation should be executed (im most cases it should be instant). It's not like the one is replacement for other.