# Pointwise function application to nested arrays

**URL:** <https://discourse.julialang.org/t/pointwise-function-application-to-nested-arrays/9083>\
**Category:** General Usage\
**Created:** [February 15, 2018, 1:26pm UTC](https://discourse.julialang.org/t/pointwise-function-application-to-nested-arrays/9083 "2018-02-15T13:26:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![axsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/axsk/32/1952_2.png) [@axsk](https://discourse.julialang.org/u/axsk)\
**Post date:** [February 15, 2018, 1:26pm UTC](https://discourse.julialang.org/t/pointwise-function-application-to-nested-arrays/9083/1 "2018-02-15T13:26:59Z")

</div>

I cannot find a convenient way to map a function over arrays of arrays.

For a simple array I can use the syntax `f.(x)`:

```julia
x=[1,2]
exp.(x)

```

For nested arrays though,

```julia
x=[[1,2],[3,4]]
exp.(x)

```

I end up with the deprecation warning

```julia
WARNING: exp(x::AbstractArray{T}) where T <: Number is deprecated, use exp.(x) instead.

```

I could use `map(x->(exp.(x)), x)`, but this is rather cumbersome and becomes even more difficult for higher levels of nesting.

What is the recommended way to deal with the pointwise application of a function to nested arrays?

---

<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:** [February 15, 2018, 1:52pm UTC](https://discourse.julialang.org/t/pointwise-function-application-to-nested-arrays/9083/2 "2018-02-15T13:52:24Z")

</div>

> [@axsk](#):
>
> I could use `map(x->exp.(x), x)`, but this is rather cumbersome and becomes even more difficult for higher levels of nesting.

I think that’s the main way to do it at this point (note that I removed an unnecessary set of extra parens); there are also variants like `[exp.(x) for x in x]`. It’s not that bad — you’re just spoiled by the dot notation. 😉

In a [future version of Julia](https://github.com/JuliaLang/julia/pull/24990) you may be able to do `map(exp.(_), x)` or `(exp.(_)).(x)`.

If you have higher levels of nesting, you may want to re-think your data structures.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [February 16, 2018, 3:32am UTC](https://discourse.julialang.org/t/pointwise-function-application-to-nested-arrays/9083/3 "2018-02-16T03:32:32Z")

</div>

Assuming the functions you would like to apply are for non-vectors,

```julia
magic(obj::Any, func::Function) = func(obj)
magic(obj::AbstractVector, func::Function) = magic.(obj, func)

```

then you could do,

```julia
objA = collect(1:2)
objB = [[1,2], [3,4]]
magic(objA, exp)
magic(objB, exp)

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 16, 2018, 1:07pm UTC](https://discourse.julialang.org/t/pointwise-function-application-to-nested-arrays/9083/4 "2018-02-16T13:07:42Z")

</div>

> [@axsk](#):
>
> I could use map(x-\>(exp.(x)), x), but this is rather cumbersome and becomes even more difficult for higher levels of nesting.

It’s not a general solution like the dot call syntax, but if you need some specific function to work recursively into nested arrays you can define it to do so.

```julia
julia> Base.exp(x::Array) = exp.(x)

julia> a=[[[0,1],[[[2,3]]]]]
1-element Array{Array{Array{Any,1},1},1}:
 Array{Any,1}[Any[0, 1], Any[Array{Int64,1}[[2, 3]]]]

julia> exp(a)
1-element Array{Array{Array{T,1} where T,1},1}:
 Array{T,1} where T[[1.0, 2.71828], Array{Array{Float64,1},1}[Array{Float64,1}[[7.38906, 20.0855]]]]

```
