Map vs list comprehension

Sorry, I was not clear. My program was behaving badly. The new “map” is fine. I really meant “bug”. It’s a long story why, but I was not getting useful error messages, only mysterious errors. Took a while to figure out.

Let’s not forget that in most cases simple broadcasting fits the needs:

myFunction.(collection_of_stuff) = map(myFunction, collection_of_stuff) = [myFunction(i) for i in collection_of_stuff]

Is there any advantage of one of these methods over the other?

I do not think so. For instance,

julia> a=[(x, x) for x in rand(100_000)];

julia> @btime map(x->first(x), a);
  91.210 μs (3 allocations: 781.34 KiB)

julia> @btime map(first, a);
  90.960 μs (3 allocations: 781.34 KiB)

julia> @btime first.(a);
  90.774 μs (5 allocations: 781.39 KiB)

The results are equivalent, both in output and time/memory.
The first two are exactly the same, when you can called directly the function, it is easier to the second option. The second one is more readable for people not used to Julia, but when you are getting used to the “dot notation”, you have tendency to prefer the last one.

3 Likes