Map a scalar function over collection of arrays

In Julia map(rad2deg, ([1],[2]) ) works. In 0.6 is is deprecated:

julia> map(rad2deg, ([1], [2]))                                                                                                        
WARNING: rad2deg{T <: Real}(x::AbstractArray{T}) is deprecated, use rad2deg.(x) instead.                                               
Stacktrace:                                                                                                                            
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70                                                                                 
 [2] rad2deg(::Array{Int64,1}) at ./deprecated.jl:57                                                                                   
 [3] map(::Base.Math.#rad2deg, ::Tuple{Array{Int64,1},Array{Int64,1}}) at ./tuple.jl:159                                               
 [4] eval(::Module, ::Any) at ./boot.jl:235                                                                                            
 [5] eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:66                                                                   
 [6] macro expansion at ./REPL.jl:97 [inlined]                                                                                         
 [7] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73                                                                     
while loading no file, in expression starting on line 0
([57.2958], [114.592])

using an array comprehension works:

julia> [rad2deg.(i) for i in ([1], [2])]                                                                                               
2-element Array{Array{Float64,1},1}:                                                                                                   
 [57.2958]                                                                                                                             
 [114.592]                                                                                                                             

as I can add the .. Is there a way to still use map?

I guess this is an example where https://github.com/JuliaLang/julia/issues/21875 would help.

Is using an anonymous function is the best workaround for now?

julia> x = ([1,2],[2,3]);

julia> @btime [rad2deg.(i) for i in x];
  191.066 ns (4 allocations: 304 bytes)

julia> @btime map(x->rad2deg.(x), x);
  90.195 ns (3 allocations: 224 bytes)

julia> @btime broadcast.(rad2deg,x);
  9.492 μs (11 allocations: 480 bytes)

Your benchmarks were misleading because they used a global variable x. When using BenchmarkTools, you have to be careful to splice in the values of globals:

julia> @btime broadcast.(rad2deg,x);
  7.395 μs (11 allocations: 480 bytes)

julia> @btime broadcast.(rad2deg,$x);
  76.135 ns (5 allocations: 256 bytes)

julia> @btime map(x->rad2deg.(x), $x);
  68.809 ns (3 allocations: 224 bytes)

julia> @btime [rad2deg.(i) for i in $x];
  102.691 ns (4 allocations: 304 bytes)

julia> @btime map.(rad2deg, $x);
  86.099 ns (5 allocations: 256 bytes)

So, the broadcast.(....) solution is just fine.

3 Likes