# Mapping over multidimensional array with eachslice

**URL:** <https://discourse.julialang.org/t/mapping-over-multidimensional-array-with-eachslice/131880>\
**Category:** New to Julia\
**Tags:** arrays, views\
**Created:** [August 27, 2025, 9:53am UTC](https://discourse.julialang.org/t/mapping-over-multidimensional-array-with-eachslice/131880 "2025-08-27T09:53:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [August 27, 2025, 9:53am UTC](https://discourse.julialang.org/t/mapping-over-multidimensional-array-with-eachslice/131880/1 "2025-08-27T09:53:45Z")

</div>

I have a data opject Array{Float64, 3} where I want to map 2nd and 3rd  
dimension a function that accepts some static arguments and takes a Vector{Float64}  
from the data object. I tried to do this with

```julia
eachslice(data, dims=(2,3)

```

but this returns views view(::Array{Float64, 3}, :, 1, 1) which my function won’t eat. Should I make the function accept views, or do I convert them beforehand and if so how?

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [August 27, 2025, 10:17am UTC](https://discourse.julialang.org/t/mapping-over-multidimensional-array-with-eachslice/131880/2 "2025-08-27T10:17:57Z")

</div>

> [@Geegee](#):
>
> Should I make the function accept views, or do I convert them beforehand and if so how?

Conversion is simple with a `collect`:

```julia-auto
julia> A = rand(4,4,4);

julia> v = @view A[:,1,1]
4-element view(::Array{Float64, 3}, :, 1, 1) with eltype Float64:
 0.8856095041969669
 0.37755696755493096
 0.9045476323189784
 0.4553299239763875

julia> collect(v)
4-element Vector{Float64}:
 0.8856095041969669
 0.37755696755493096
 0.9045476323189784
 0.4553299239763875

```

It creates a copy, thus allocates memory.

What are you doing in your function which does not work with views?

---

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [August 27, 2025, 10:25am UTC](https://discourse.julialang.org/t/mapping-over-multidimensional-array-with-eachslice/131880/3 "2025-08-27T10:25:01Z")

</div>

I think it could work with views. I just assigned it  
like f(a::Vector{Float64}), how do I make a proper type  
signature to allow views?

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [August 27, 2025, 10:26am UTC](https://discourse.julialang.org/t/mapping-over-multidimensional-array-with-eachslice/131880/4 "2025-08-27T10:26:09Z")

</div>

You can try `f(a::AbstractVector{Float64})`, or just `f(a)`.

---

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [August 27, 2025, 11:13am UTC](https://discourse.julialang.org/t/mapping-over-multidimensional-array-with-eachslice/131880/5 "2025-08-27T11:13:55Z")

</div>

I tried it and it seems I have to copy the array. I guess it is because the underlying  
function uses PyCall and then it can not give just a reference to the array.  
Nevertheless I am very glad to have found AbstractArray thanks to you as I  
struggled with this sometime in the past.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [August 27, 2025, 4:55pm UTC](https://discourse.julialang.org/t/mapping-over-multidimensional-array-with-eachslice/131880/6 "2025-08-27T16:55:10Z")

</div>

You might want `mapslices(f, data; dims=1)`, as this makes a slice which should be an Array, not a view. Or `mapslices(x -> f(x, extra), data; dims=1)` to pass extra arguments to `f`.

It will re-use the same Array for the slice, so might be slightly more efficient than `collect`ing the view. Note that it has the opposite convention to `eachslice` for what the `dims` means.
