On a user level, I prefer to use map
when f
is somehow complicated. It’s usually either when you need to write it as an anonymous function, or when you can’t use syntax sugar and have to call methods explicitly, or when function is so complicated that you want to use do
syntax.
Example 1:
f(x, y) = x + y
v = [1, 2, 3]
map(x -> f(x, 1), v)
# vs
(x -> f(x, 1)).(v)
Example 2
v = [(1, 1), (2, 2), (3, 3)]
map(x -> x[2], v)
# vs
getindex.(v, 2)
Example 3:
v = [(0, 1), (1, 2), (2, 3)]
map(v) do x
println(v[1])
v[2]*v[2]
end
In all other cases, broadcasting is usually easier to read and use.