# Map a scalar function over collection of arrays

**URL:** https://discourse.julialang.org/t/map-a-scalar-function-over-collection-of-arrays/4634
**Category:** General Usage
**Created:** [July 4, 2017, 9:23am UTC](https://discourse.julialang.org/t/map-a-scalar-function-over-collection-of-arrays/4634 "2017-07-04T09:23:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [July 4, 2017, 9:23am UTC](https://discourse.julialang.org/t/map-a-scalar-function-over-collection-of-arrays/4634/1 "2017-07-04T09:23:57Z")

</div>

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

```julia
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
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`?

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [July 4, 2017, 10:42am UTC](https://discourse.julialang.org/t/map-a-scalar-function-over-collection-of-arrays/4634/2 "2017-07-04T10:42:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ohsonice](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ohsonice](https://discourse.julialang.org/u/ohsonice)
#### Post date: [July 4, 2017, 4:33pm UTC](https://discourse.julialang.org/t/map-a-scalar-function-over-collection-of-arrays/4634/3 "2017-07-04T16:33:54Z")

</div>

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

```julia
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)

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 4, 2017, 6:49pm UTC](https://discourse.julialang.org/t/map-a-scalar-function-over-collection-of-arrays/4634/4 "2017-07-04T18:49:01Z")

</div>

> [@ohsonice](#):
>
> Is using an anonymous function is the best workaround for now?

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
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.
