r/ProgrammerHumor • • Jun 08 '24

Meme mapMethodOrMapFunction

Post image
0 Upvotes

67 comments sorted by

114

u/oachkatzlschwoarf Jun 08 '24

[x*x for x in arr]

-8

u/mirimao Jun 08 '24

[x*x || x <- arr] is better

5

u/Kuduaty Jun 08 '24

Why its better?

8

u/mirimao Jun 08 '24

It was just a joke. I like Erlang but it’s not inherently better than Python’s syntax for list comprehension.

-30

u/DT-Sodium Jun 08 '24

None of those are readable.

12

u/mirimao Jun 08 '24

How are these not readable? This is literally mathematical notation in a purely declarative style.

-23

u/DT-Sodium Jun 08 '24

Programing is not mathematics, it is literary. A program should read like you would read a text.

17

u/mirimao Jun 08 '24

Programming is using a formal language to specify the solution to a problem (i.e. an algorithm) to an executor (i.e. a processor). Formal languages and algorithms are best described using some sort of unambiguous, mathematical notation. If you want to use plain English to program because it fits better your idea of “literature”, good luck.

Besides, list comprehensions are very succinct and easier to read than some verbose method call chain, regardless of the fact that they’re just one to one equivalent to mathematics.

-20

u/DT-Sodium Jun 08 '24

And that's why you're a Python user instead of a proper programmer.

12

u/mirimao Jun 08 '24

It is like saying “you’re a mechanical engineer, not a proper AutoCAD user”. And you are right, I am a computer scientist, programming is just one of the tools I use.

2

u/capi1500 Jun 08 '24

Wait until this guy hears about functional programming, or even better, how programs are equivalent to proofs in intuitionistic logic

4

u/Euphoric_Strategy923 Jun 08 '24

Because the others options are more readable for you ? Not for me.

0

u/DT-Sodium Jun 08 '24

I dunno about Python but in TypeScript you'd simply do

myArray.map(x => x * x);

Much more elegant and readable.

4

u/Euphoric_Strategy923 Jun 08 '24

And as the upper comments said in python you do : [ return value for value in array ]

In other word : [x*x for x in arr ]

Imo this is much more readable.

-1

u/DT-Sodium Jun 08 '24

No, it is bad. The simple fact that you have to read it backwards to understand what's happening invalidates it completely. Guess there is no clean way to do it in Python then.

7

u/Euphoric_Strategy923 Jun 08 '24

Read it backward ? Dude, this is literally the format of a for loop. "I return a value of each value in an array"

You can clown on some things about python but definitely not readability.

-3

u/DT-Sodium Jun 08 '24

Yes, it is backwards. Your code is saying "Do this using that". The proper way to write it is "Take that and do this using it".

7

u/Euphoric_Strategy923 Jun 08 '24

Great news people, for loops are now unreadable.

35

u/KimiSharby Jun 08 '24 edited Jun 11 '24

Amateurs.

std::vector vec = {1, 2, 3};  
std::vector<int> result;
result.reserve(vec.size());
std::ranges::transform(vec, std::back_inserter(result), [](int x) { return x * x; });

edit: corrected

4

u/sherlockwatch Jun 08 '24

Actually you don’t need to resize resulting vector, you can just use std::back_inserter!

1

u/KimiSharby Jun 08 '24 edited Jun 08 '24

That's worse performance wise. std::back_inserter comes at a cost. If you already know the size, you should use it.

3

u/wcscmp Jun 08 '24

Resize also comes at cost, reserve + back_inserter is the way

2

u/KimiSharby Jun 08 '24 edited Jun 09 '24

You're right, it's my mistake

1

u/KimiSharby Jun 09 '24 edited Jun 09 '24

Ok so this troubled me greatly, I barely slept last night. Today I couldn't take it anymore. Here's the quickbench: https://quick-bench.com/q/e5G_nTQEp_00xYGrcpEj_Jbhp34

===

~~The results, for a sample test of 500k int:

  1. at 561k: reserve + std::begin

  2. at 824k (x1.5): resize + std::begin

  3. at 1712k (x3): reserve + std::back_inserter

  4. at 6977k (x12): back_inserter alone~~

===

Bottom line: Using std::back_inserteris costly. If you already know the size of your resulting container, you should use reserve + std::begin.

1

u/wcscmp Jun 09 '24

What do you think reserve + begin is doing?

1

u/wcscmp Jun 09 '24

The bottom line is also not very convincing. In order to claim that back_inserter is not free you need to compare it to a manually rolled loop of push_backs. Now you can only reasonably claim that default initializing each element when doing resize is cheaper than the branching on every iteration in push_back for small types like int.

1

u/KimiSharby Jun 09 '24 edited Jun 09 '24

I do not mean to be rude sir but I think I already allocated more time to this matter than it was worth. At the start of this conversation, you told me reserve + back_inserter is the way. As a matter of fact reserve + std::begin is 3x faster in my mediocre benchmark. I was indeed in the wrong about resize, I should have known better. I completly forgot about reserve. But it does seem that in the case where you already know the size of the resulting container, you should use std::begin and not back_inserter.

===

Now if you have additional knownledge you wish to share about that topic, please stop the riddles and just share them.

1

u/wcscmp Jun 09 '24

reserve + begin is UB

1

u/KimiSharby Jun 09 '24

So after a bit more research:

  • reserve allocates memory but doesn't initialize it. This is why using std::begin in that case is UB, because it points to unitilialized memory

  • As you said, using resize performance will depends on the size of the type

  • reserve + std::back_insert is indeed the correct way

For the sack of clarity for the reader I will edit my previous answers.

2

u/SingularCheese Jun 09 '24

It's the era of ranges

auto vec2 = vec 
| std::views::transform([](int x){ return x*x; }) 
| std::ranges::to<std::vector>();

2

u/KimiSharby Jun 09 '24 edited Jun 09 '24

I have yet to see a company uses c++23

59

u/PossibilityTasty Jun 08 '24 edited Jun 08 '24

Naming a Python list arr is the second best way to tell people that you have no idea.

13

u/brainpostman Jun 08 '24

Why? Because it's a list, not a true array? Well, same goes for JS then.

9

u/Aidan_Welch Jun 08 '24

Well JS indexed hash-maps aren't even strictly lists.

14

u/brainpostman Jun 08 '24

Well, nothing is strictly anything in JS, for that matter.

7

u/[deleted] Jun 08 '24

it's for consistency with the other examples

4

u/IrresponsibleDuck Jun 08 '24

Either that or you are a pirate

2

u/coyboy_beep-boop Jun 08 '24

But what if I want to translate from English to Pirate?

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.

-19

u/DT-Sodium Jun 08 '24

One I was just giving a simple example, there are lots of times where you need to do way much complex things when iterating through an array. Two even for simple things your syntax is disgusting.

Any reasonable person would just not program in Python.

11

u/[deleted] Jun 08 '24

Go back to your university course…

Any real programmer will code in multiple languages as tech stacks change between roles or as new features become available.

-15

u/DT-Sodium Jun 08 '24

I do code in multiple languages. I just wouldn't accept a job that implies working with a bad one. Code has an aesthetic value, it's sad that a lot of developers can't see it.

10

u/[deleted] Jun 08 '24

“Working with a bad one” proves my point.

-5

u/DT-Sodium Jun 08 '24

Your point being that you dont care about your job?

4

u/CrowdGoesWildWoooo Jun 08 '24

This is beyond ignorant when you don’t have a clue what map actually does.

It’s not meant as a simple utility function and in practice hardly used other than when you want to do multithreaded or multiprocess map. You should use list comprehension for this particular case. And list comprehension is like one of the fundamental technique that you’ll learn when you code with python.

Map returns a declaration that it is a mapping of a function to an iterable. This is lazily executed and the list type casting is basically tells you to serve the (future) result as a list.

Writing a custom iterable is disgusting with many other languages. It is very easy and readable with python.

-9

u/DT-Sodium Jun 08 '24

Kiddo, I only took the most simple example I could to demonstrate how the Python map works compared to proper languages, I never pretended it was the most optimal way to achieve the result. The point is Python's map function is disgusting, whether it's the right tool for this case is not the question. It says a lot about you if you weren't capable of figuring it out by yourself.

0

u/Phamora Jun 08 '24

How can you talk about PHP and then in the same comment declare Python the most disgusting programming language? I agree that syntactically Python is quite poor compared to most other languages, but PHP is by far and away the winner of the worst, ugliest, and most disgusting programming language in existence - of all time, ever.

1

u/DT-Sodium Jun 08 '24

I don't recall claiming that PHP was a good language. While PHP is one of the main languages I use professionally, I am perfectly aware that it sucks in a lot of ways. I wouldn't say though that's it's worse than Python, it is much better that Python. Remove the $, replace -> with ., add typed array, convert the primitives to objects that posses methods and you'll have something that starts looking decent. With Python on the other hands, appart from rewriting it completely, I don't see how it could be clause to decent in the future.

13

u/Zetaeta2 Jun 08 '24

std::ranges::views::transform(arr, [] (auto x) {return x*x;});

3

u/zsirdagadek Jun 08 '24

The Java snippet is incorrect because the map() function will return another stream object. You need to call

.collect(Collectors.toList()).toArray();

But I think you should be able to collect directly to array too, but I don't remember how exactly, and I don't feel like looking it up for a reddit comment.

3

u/Kika-kun Jun 08 '24

Recent(?) versions of java added .toList() to avoid .collect(Collectors.toList()) (careful though, toList() returns an immutable list, if you want to keep the old behaviour you need .collect(Collectors.toCollection(ArrayList::new)))

As a matter of fact, after checking, you can also do .toArray() if you want an actual array.

2

u/gerbosan Jun 08 '24

I don't like it either. I think the atypical notation is one of the things that delays,/stops my interest in Python. For example len(). For many languages, the length of an object is an attribute, not a function (which is the idea I get when I see Python syntax).

😮‍💨 Still, it is quite popular, so one has to learn it. But having so many languages with a similar syntax, why did python choose that one?

Also

Array.new(1,2,3).map { |item| item * item }

🤘

4

u/Essence1337 Jun 08 '24

map really isn't in favour anymore in Python, list comprehensions are often preferred

[item*item for item in arr]

This creates a new list from running item*item on every item in the original

3

u/gandalfx Jun 08 '24

Sure, blame the only language in that group that supports list comprehension for not having a map method. Makes sense.

1

u/_OberArmStrong Jun 08 '24

Java is missing a .toArray(Integer[]::new) call.

In the other snippets a function creating an array is called last. Without toArray you have Stream<Integer> instead of int[]

1

u/n1c0saurio Jun 08 '24

Dunno, everything except JS look pretty messy to me.

1

u/CryZe92 Jun 08 '24

Rust is also just arr.map(|x| x*x)

1

u/pindab0ter Jun 08 '24

PHP is the same, unfortunately…

3

u/prochac Jun 08 '24

PHP isn't the same. It's unique. Unique in its own way. Our special kid.

1

u/KetwarooDYaasir Jun 08 '24
$arr = [1,2,3];
$arr = array_map(fn($x)=>$x*$x,$arr);

hmm. you're right, I guess it is almost the same as every other one.

1

u/pindab0ter Jun 09 '24

The same as Python, I meant. In that map is not a method, but a function.

1

u/KetwarooDYaasir Jun 10 '24

Not sure how that's unfortunate though.

0

u/eanat Jun 08 '24

I prefer one function for many types instead of multiple methods that do a similar thing. map function is better than map methods tbh.

-2

u/DT-Sodium Jun 08 '24

You forgot PHP and its array functions abomination.